##
## global.tcl --
##
##     Global variables definition.   
##
## Copyright (C) 2002 Stacho Mudrak
## 
## $Date: $
## $RCSfile: $
## $Revision: $
##
## -------------------------------------------------------------------- 
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2 of the License, or
## any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
## 
## You should have received a copy of the GNU General Public License
## along with this program; if not, write to the Free Software
## Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA
## --------------------------------------------------------------------

package require msgcat
catch {namespace import ::msgcat::mc}  

if {[string equal -nocase [::msgcat::mclocale] c]} {
  ::msgcat::mclocale en
}

set xth(destroyed) 0
set xth(init_logs) {}
set xth(prj,name) "therion"
set xth(prj,title) "therion user interface"
set xth(gui,main) ".xth"
set xth(gui,about) ".xth_about"
set xth(gui,bacw) ".xth_bac"
set xth(gui,dbg) ".xth_dbg"
set xth(gui,help) ".xth_help"
set xth(gui,message) ".xthmsg"
#set xth(gui,minsize) {480 300}
set xth(gui,minsize) {720 576}
set xth(gui,init_app_normalized) 1
set xth(gui,balloons) 0
set xth(gui,toolbar) 1
set xth(te,template) {}

set xth(gui,ctrlBtnFg) white
set xth(gui,ctrlBtnBg) darkBlue
set xth(gui,ctrlBtnActiveFg) black
set xth(gui,ctrlBtnActiveBg) lightBlue

set xth(kb_control) Control
set xth(kb_meta) Meta
set xth(gui,compshow) 0
set xth(gui,compcmd) "therion"
set xth(gui,appctrlcmd) {}
set xth(gui,auto_save) 0
set xth(gui,auto_backup) 0
set xth(gui,auto_backup_ext) "~"
set xth(gui,check_update) 1


set xth(encodings) { iso8859-1 iso8859-2 iso8859-5 iso8859-7 utf-8 cp1250 cp1251 cp1252 cp1253}
set xth(kbencodings) {utf-8 iso8859-1 iso8859-2 cp1250 cp1251 macCentEuro unicode}
if {[lsearch -exact $xth(kbencodings) [encoding system]] < 0} {
  lappend xth(kbencodings) [encoding system]
}

# The collation aware sort code used by xtherion takes a map in which collation differences can be 
# listed as {from to from to...}, sorts the mapped items, and retrieves only the original elements.
#
# Examples:
#
# Portuguese:
# % lsort {ab ãc ae ãd}
# ab ae ãc ãd
# % collatesort {ab ãc ae ãd} {ã a}
# ab ãc ãd ae
#
# Spanish (ll sorts after lz):
# % collatesort {llano luxación leche} {ll lzz}
# leche luxación llano
#
# German (umlauts sorted as if "ä" was "ae"):
# % lsort {Bar Bär Bor}
# Bar Bor Bär
# % collatesort {Bar Bär Bor} {ä ae}
# Bär Bar Bor
#
set xth(collation-maps,pt) { á a à a ã a â a é e ê e í i î i ó o ô o õ o ú u ù u û u ç c \
  Á A À A Ã A Â A É E Ê E Í I Î I Ó O Ô O Õ O Ú U Û U Ç C }

set xth(length_units) {m cm in ft yd}
set xth(angle_units) {deg min grad}
set xth(point_types) {}
set xth(line_types) {}
set xth(scrap_projections) {plan elevation extended none}
set xth(app,te,filetypes) {    
  {{Therion files}       {.th}}    
  {{2D therion files}       {.th2}}    
  {{All files}       {*}}    
}
set xth(app,te,fileext) {.th}

set xth(app,me,filetypes) {    
  {{Therion 2D files}       {.th2}}    
  {{Therion files}       {.th}}    
  {{All files}       {*}}    
}

set xth(app,cp,filetypes) {    
  {{Therion config files }       {.thcfg .thconfig thconfig*}}    
  {{All files}       {*}}    
}

set xth(icmds) {survey}
set xth(cmds) {scrap centerline grade line area map layout surface}

set xth(datafmt,unknown) {4s}                    
set xth(datafmt,station) {4s}                    
set xth(datafmt,length) {6.2fx {-}}             
set xth(datafmt,counter) {6.2f {-}}            
set xth(datafmt,bearing) {5.1fx {-}}             
set xth(datafmt,gradient) {{5.1fx} {up down -}} 
set xth(datafmt,depth) {6.2fx {-}}            
set xth(datafmt,dimensions) {4.1fx {-}}             

set xth(gui,initdir) ""
set xth(app,active) ""
set xth(app,list) {}
set xth(app,all,tbwidth) 256
set xth(app,all,relw) 380
set xth(app,all,wmwd) 180
set xth(app,all,wpsw) 1

set xth(app,fencoding) utf-8
set xth(app,sencoding) utf-8

# autodetect some options
frame .def
scrollbar .def.scrollbar
text .def.text
if {[catch {.def.text configure -undo 1}]} {
  set xth(gui,text_undo) 0;
} else {
  set xth(gui,text_undo) 1;
}
label .def.label

set xth(gui,sbwidth) [.def.scrollbar cget -width]
set xth(gui,sbwidthb) [.def.scrollbar cget -borderwidth]
set xth(gui,lfont) [.def.label cget -font]
set xth(gui,efont) [.def.text cget -font]
set xth(gui,ecolorbg) black
set xth(gui,ecolorfg) green
set xth(gui,escolorbg) black
set xth(gui,escolorfg) red
set xth(gui,ecolorselbg) green
set xth(gui,ecolorselfg) black
set xth(gui,selfg) white
set xth(gui,selbg) darkBlue
set xth(gui,etabsize) 2
set xth(gui,controlk) Ctrl

set xth(import,size) 1024.0
set xth(import,mind) 8.0
set xth(import,dxf,scale) 1.0
set xth(import,dxf,cs) {}
set xth(import,svg,scale) 1.0
set xth(import,svg,cs) {}
set xth(import,default,scale) {}
set xth(import,default,cs) {}

set xth(gui,xvi_grid_clr) #00D0D0
set xth(gui,xvi_walls_fptn) gray12
set xth(gui,xvi_walls_fclr) gray80
set xth(gui,xvi_walls_oclr) gray60
set xth(gui,xvi_shot_clr) gray50
set xth(gui,xvi_station_fclr) black
set xth(gui,xvi_station_oclr) black
set xth(gui,xvi_station_size) 3.0
set xth(gui,xvi_sketch_line_width) 1

set xth(gui,me,nozoom) 1

destroy .def
# end of options autodetection

# map editor settings

# translate types in dropdown menus
set xth(gui,me,type_dropdown_trans) 1

# reset options string on type change
set xth(gui,me,type_reset_options) 1

#########################################
# SCRAP
# size of scrap scaling square
set xth(gui,me,scrap,psize) 4


#########################################
# POINT
# size of point
set xth(gui,me,point,psize) 4

#########################################
# LINE
# size of line point
set xth(gui,me,line,psize) 3
# line width
set xth(gui,me,line,width) 3
# size of line control point
set xth(gui,me,line,cpsize) 3
# width of line between point and control point
set xth(gui,me,line,clwidth) 2
# size of start line tick
set xth(gui,me,line,ticksize) 15
# width of start line tick
set xth(gui,me,line,tickwidth) 3
# simplification limit for lines. Larger simp_limit = less points; smaller simp_limit = more points and smoother line
set xth(gui,me,line,simp_limit) 10

# for color names, see http://wiki.tcl.tk/37701
set xth(gui,me,activefill) red
set xth(gui,me,pasivefill) green
set xth(gui,me,controlfill) blue
set xth(gui,me,highlightfill) cyan
set xth(gui,me,unselectedfill) lightgray
set xth(gui,me,wallcolor) blue
set xth(gui,me,pitcolor) magenta
set xth(gui,me,slopecolor) gold
set xth(gui,me,rockcolor) purple
set xth(gui,me,bordercolor) turquoise
set xth(gui,me,stationcolor) darkorange

set xth(gui,me,typelistwidth) 16
set xth(gui,rmb) 3

set xth(gui,bindinsdel) 1
set xth(gui,me,pointsizectrl) 0

# platform dependent settings
switch -- $tcl_platform(platform) {
  unix {
    set xth(gui,sbwidth) 9
    set xth(gui,sbwidthb) 1
    set xth(gui,efont) {fixed -20}
    set xth(gui,platform) unix
    set xth(gui,cursor) top_left_arrow
    set xth(gui,compshow) 1
    if {[string equal $tcl_platform(os) Darwin]} {
        set xth(kb_meta) Meta
        set xth(kb_control) Alt
        set xth(gui,controlk) Cmd
        set xth(gui,platform) macintosh
        set xth(gui,cursor) arrow
        set xth(gui,bindinsdel) 0
        set xth(gui,ctrlBtnFg) darkBlue
        set xth(gui,ctrlBtnBg) white
        set xth(gui,ctrlBtnActiveFg) darkBlue
        set xth(gui,ctrlBtnActiveBg) lightGray
    }
  }
  windows {
    package require registry
    set xth(win32registry) {HKEY_LOCAL_MACHINE\SOFTWARE\Therion}
    if {[catch {
      set xth(gui,compcmd) "\"[file join [registry -64bit get $xth(win32registry) InstallDir] therion.exe]\""
    }]} {
      set xth(win32registry) {HKEY_CURRENT_USER\SOFTWARE\Therion}
      catch {
	set xth(gui,compcmd) "\"[file join [registry -64bit get $xth(win32registry) InstallDir] therion.exe]\""
      }      
    }
    
    catch {
      if {[registry -64bit get {HKEY_LOCAL_MACHINE\SOFTWARE\Therion} AppCtrl]} {
	set xth(gui,appctrlcmd) "[file join [registry -64bit get $xth(win32registry) InstallDir] bin appctrl.exe]"
      }
    }
    regsub -all {\/} $xth(gui,compcmd) {\\\\} xth(gui,compcmd)
    set xth(gui,efont) "Courier 16 roman bold"
    set xth(gui,platform) windows
    set xth(gui,cursor) arrow
    set xth(app,sencoding) [encoding system]
    set xth(gui,bindinsdel) 0
    if {[catch {
      set fid [open "|cmd.exe /c" r]
      read $fid;
      close $fid
    }]} {
      set xth(gui,compcmd) "command.com /c $xth(gui,compcmd)"
    } else {
      set xth(gui,compcmd) "cmd.exe /c $xth(gui,compcmd)"
    }
  }
  macintosh {
    set xth(kb_meta) Meta
    set xth(kb_control) Alt
    set xth(gui,controlk) Cmd
    set xth(gui,platform) macintosh
    set xth(gui,cursor) arrow
    set xth(gui,bindinsdel) 0
    set xth(app,sencoding) utf-8
  }
}


switch -- $tcl_platform(os) {
  Darwin {
    set xth(gui,rmb) 2
  }
}


# end of platform dependent settings

# words to translate
set tmp [mc "theme basic-symbols"]
set tmp [mc "theme passage-fills"]
set tmp [mc "theme speleothems"]
set tmp [mc "theme equipement"]
# This line type is special. It even doesn't appears on map legend.
# Couldn't find a better place to include this
set tmp [mc "line arrow"]

set xth(me,themes) {
  {theme basic-symbols}
    {point gradient}
    {point height}
    {point passage-height}
    {point entrance}
    {point label}
    {point station}
    {point continuation}
    {point water-flow}
    {point air-draught}
    {line wall}
    {line chimney}
    {line ceiling-step}
    {line floor-step}
    {line pit}
    {line floor-meander}
    {line ceiling-meander}
    {line contour}
    {line label}
    {line overhang}
    {line water-flow}
    {area water}
    {area sump}
    {area bedrock}
  {theme passage-fills}
    {point blocks}
    {point archeo-material}
    {point clay}
    {point debris}
    {point breakdown-choke}
    {point flowstone-choke}
    {point low-end}
    {point narrow-end}
    {point guano}
    {point ice}
    {point paleo-material}
    {point pebbles}
    {point raft}
    {point raft-cone}
    {point root}
    {point sand}
    {point snow}
    {point vegetable-debris}
    {point water}
    {line border}
    {line border:invisible}
    {line rock-border}
    {line rock-edge}
    {area blocks}
    {area clay}
    {area debris}
    {area ice}
    {area pebbles}
    {area sand}
    {area snow}
  {theme speleothems}
    {point anastomosis}
    {point aragonite}
    {point cave-pearl}
    {point crystal}
    {point curtain}
    {point disk}
    {point flowstone}
    {point flute}
    {point gypsum}
    {point gypsum-flower}
    {point helictite}
    {point karren}
    {point moonmilk}
    {point pillar}
    {point popcorn}
    {point rimstone-dam}
    {point rimstone-pool}
    {point scallop}
    {point soda-straw}
    {point stalactite}
    {point stalagmite}
    {point wall-calcite}
    {line flowstone}
    {line moonmilk}
    {area flowstone}
    {area moonmilk}
  {theme equipement}
    {point anchor}
    {point bridge}
    {point camp}
    {point fixed-ladder}
    {point no-equipment}
    {point rope}
    {point rope-ladder}
    {point steps}
    {point traverse}
    {line rope}
}





set xth(about,image_data) {
[encoded data, 6100 characters]
GRAAOw==
====
}

proc xth_incr_station_name {oname iii} {
  if {[regexp {^(\S+)(\@\S+)$} $oname dumm stname svname]} {
    set oname $stname
  } else {
    set svname {}
  }
  if {[regexp {^0*(\d+)$} $oname dum s2]} {
    set numlen [string length $dum]
    incr s2 $iii
    if {[string length $s2] < $numlen} {
      set s2 [format [join [list "%0" $numlen d] ""] $s2]
    }
    return "$s2$svname"
  }
  if {[regexp {^(.*\D)(0*(\d+))$} $oname dumm s1 s3 s2]} {
    set numlen [string length $s3]
    incr s2 $iii
    if {[string length $s2] < $numlen} {
      set s2 [format [join [list "%0" $numlen d] ""] $s2]
    }
    return "$s1$s2$svname"
  } 
  if {[regexp {^(.*)(\D)$} $oname dumm s2 s1]} {
    set avs1 [scan $s1 %c]
    if {(($avs1 >= 65) && ($avs1 < 90)) || (($avs1 >= 97) && ($avs1 < 122))} {
      incr avs1
      return "$s2[format %c $avs1]$svname"
    }
  } 
  if {[regexp {^(0*(\d+))(\D.*)$} $oname dumm s3 s2 s1]} {
    set numlen [string length $s3]
    incr s2 $iii
    if {[string length $s2] < $numlen} {
      set s2 [format [join [list "%0" $numlen d] ""] $s2]
    }
    return "$s2$s1$svname"
  } 
  return "$oname$svname"
}


proc xth_getdir {fname} {
  set d [file dirname $fname]
  if {[file exists $d] && [file isdirectory $d]} {
    return $d
  } 
  return {}
}

