### Copyright (C) 1995 Jeppe Buk (buk@imada.sdu.dk)
### 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
### (at your option) 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., 675 Mass Ave, Cambridge, MA 02139, USA.

proc fonts {} {
  # Fonts
  Desc "Configuration of fonts used in different elements"
  ShortDesc "Font configuration"

  set tempWin [makeTempWindow Retrieving]
  label $tempWin.fonts -text "Parsing fonts, please wait..."
  label $tempWin.fontsCount -text "0"
  pack $tempWin.fonts
  pack $tempWin.fontsCount
  update
  set fontsCount 0
  set names [getFonts]
  
  if {$names != {}} {
    # The fonts have been found
    set aliases {{No alias - use criterias above}}
    global trueNames
    set trueNames {}
    foreach var {fndry fmly wght slant swdth adstyl pxlsz ptSz resx \
		     resy spc avgWdth rgstry encdng} {
      set $var {}
    }
    
    foreach font $names {
      set fontNameList [split $font -]
      if {[llength $fontNameList] != 15} {
	# The font name is an alias
	if {[lsearch -exact $aliases $font] == -1} {
	  lappend aliases $font
	}
      } else {
	# The font name is a full X font name
	lappend trueNames $font
	set index 1
	foreach var {fndry fmly wght slant swdth adstyl pxlsz ptSz resx \
			 resy spc avgWdth rgstry encdng} {
	  if {[lsearch -exact [set $var] [lindex $fontNameList $index]] == -1} {
	    lappend $var [lindex $fontNameList $index]
	  }
	  incr index
	}
      }
      incr fontsCount
      if {[expr "$fontsCount % 10"] == 0} {
	$tempWin.fontsCount configure -text $fontsCount
	update
      }
    }
    foreach var {fndry fmly wght slant swdth adstyl spc rgstry encdng} {
      set $var "* [lsort [set $var]]"
    }
    foreach var {pxlsz ptSz resx resy avgWdth} {
      set $var [removeNonIntegers [set $var]]
      set $var "* [lsort -integer [set $var]]"
    }
    destroy $tempWin
    
    Label fontCategory -font --*-helvetica-medium-r-*-*-18-*-*-*-*-*-*-* \
	-help "This label shows you which font you are changing"
    Line fontLine
    CheckBox fontCheck -text "Check font pattern" \
	-help "If this option is checked the module will check if the" \
	"current X font pattern matches a font on your system. This may" \
	"take a while..."
    CheckBox showFont -text "Show font sample"
    Frame checkBoxes -entries {fontCheck showFont}
    Label currRegExpr -help "This is the font pattern you have selected so far"
    Label checkLabel -help "This label shows you if the font pattern matches" \
	"a font on your system, if you have selected the checkbox above."
    foreach var {fndry fmly wght slant swdth adstyl pxlsz ptSz resx \
		     resy spc avgWdth rgstry encdng} {
      Label label_$var -text $var -help "This is the $var of the font"
      Menu menu_$var -entries [set $var] -help "This is the $var of the font"
      Frame frame_$var -entries "label_$var menu_$var" -orient top
    }
    Frame frame1 -entries {
      frame_fndry frame_fmly frame_wght frame_slant frame_swdth frame_adstyl \
	  frame_pxlsz}
    Frame frame2 -entries {
      frame_ptSz frame_resx frame_resy frame_spc frame_avgWdth frame_rgstry \
	  frame_encdng}
    
    ListBox aliasesBox -width 70 -text "Aliases" -entries $aliases \
	-help "These are font aliases. Use one of these, if you don't like" \
	"the X font selection above"
    
    ExtEntry fontsEntry -noscissor 1 -entries {
      fontCategory fontLine checkBoxes currRegExpr checkLabel frame1 \
	  frame2 aliasesBox} \
	-orient top \
	-default {
	  {"Choose menu font" 0 "-*-*-*-*-*-*-*-*-*-*-*-*-*-*" \
	       "Font match: not checked"}
	  {"Choose window font" 0 "-*-*-*-*-*-*-*-*-*-*-*-*-*-*" \
	       "Font match: not checked"}
	  {"Choose icon font" 0 "-*-*-*-*-*-*-*-*-*-*-*-*-*-*" \
	       "Font match: not checked"}
	  {"Choose pager font" 0 "-*-*-*-*-*-*-*-*-*-*-*-*-*-*" \
	       "Font match: not checked"}
	} \
	-maxentries 4 -count 1 \
	-help "On this page you can select the fonts to be used in menus, on" \
	"windows, under icons and in the internal pager.\nSelect which font" \
	"to change with the scroll bar to the right."
  } else {
    error "Could not execute 'xlsfonts' - update your PATH"
  }

  SaveVars trueNames
  
  Init {
  }
  
  Change {
    if {[string match "menu_*" $changeElm] == 1} {
      set regExpr {}
      foreach var {fndry fmly wght slant swdth adstyl pxlsz ptSz resx \
		       resy spc avgWdth rgstry encdng} {
	append regExpr -[set menu_${var}(name)]
      }
      set currRegExpr $regExpr
    }
    if {[string match "menu_*" $changeElm] == 1 || $changeElm == "fontCheck"} {
      if {$fontCheck == 1} {
	set match 0
	foreach font $trueNames {
	  if {[string match $currRegExpr $font] == 1} {
	    set match 1
	    break
	  }
	}
	if {$match == 0} {
	  set checkLabel "Font match: no!"
	} else {
	  set checkLabel "Font match: yes"
	}
      } else {
	set checkLabel "Font match: not checked"
      }
    }
    if {$changeElm == "showFont" && $showFont == 1} {
      set showFont 0
      if {[winfo exists .fontSample] == 0} {
	toplevel .fontSample
	wm title .fontSample "Font sample"
	label .fontSample.sample
	pack .fontSample.sample
	button .fontSample.ok -text Close -command {destroy .fontSample}
	pack .fontSample.ok
      }
      if {$aliasesBox(name) == {} || $aliasesBox(index) == 0} {
	  set fontName $currRegExpr
	} else {
	  set fontName $aliasesBox(name)
	}
      .fontSample.sample configure \
	  -text "This is a sample of the font\n$fontName" \
	  -font $fontName
    }
  }
  
  Save {
    forevery fontsEntry {
      switch $fontCategory {
	"Choose menu font"   {set fontType Font}
	"Choose window font" {set fontType WindowFont}
	"Choose icon font"   {set fontType IconFont}
	"Choose pager font"  {set fontType PagerFont}
      }
      if {$currRegExpr != {-*-*-*-*-*-*-*-*-*-*-*-*-*-*} || \
	      $aliasesBox(name) != {} && $aliasesBox(index) != 0} {
	if {$aliasesBox(name) == {} || $aliasesBox(index) == 0} {
	  print "$fontType\t$currRegExpr"
	} else {
	  print "$fontType\t$aliasesBox(name)"
	}
      }
    }
  }
}
