#
# This file is part of:
#
#  gpsman --- GPS Manager: a manager for GPS receiver data
#
# Copyright (c) 1998-2013 Miguel Filgueiras migfilg@t-online.de
#
#    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 3 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.
#
#  File: isolatin1.tcl
#  Last change:  6 October 2013
#

### ISO Latin1 characters; mainly from procs written by Luís Damas

# consulting this file may cause problems in non-western Linux installations

proc ISOBindings {w} {
    # set bindings to force isolatin1 character interpretation
    global tcl_platform

    if { $tcl_platform(platform) == "windows" } {
	# no dead keys in MS-Windows, it seems
	set binds {{asciitilde ~ {a ã} {c ç} {n ñ} {o õ}
	                         {A Ã} {C Ç} {N Ñ} {O Õ}}
	           {quoteright ' {a á} {e é} {i í} {o ó} {u ú}
		                 {A Á} {E É} {I Í} {O Ó} {U Ú}}
	           {asciicircum ^ {a â} {e ê} {o ô}}
	           {quoteleft ` {a à} {e è}}}
    } else {
	# not supported:
	#    dead_diaresis
	set binds {{asciitilde ~ {a ã} {c ç} {n ñ} {o õ} {asciitilde ¸}
	                         {A Ã} {C Ç} {N Ñ} {O Õ}}
		   {dead_cedilla ¸ {c ç} {C Ç}}
	           {dead_tilde ~ {a ã} {c ç} {n ñ} {o õ} {dead_tilde ¸}
	                         {A Ã} {C Ç} {N Ñ} {O Õ}}
		   {quoteright ' {a á} {c ç} {e é} {i í} {o ó} {u ú}
		                 {A Á} {C Ç} {E É} {I Í} {O Ó} {U Ú}}
		   {dead_acute ' {a á} {e é} {i í} {o ó} {u ú}
		                 {A Á} {E É} {I Í} {O Ó} {U Ú}}
	           {apostrophe ' {a á} {e é} {i í} {o ó} {u ú}
		                 {A Á} {E É} {I Í} {O Ó} {U Ú}}
		   {dead_circumflex ^ {A Â} {a â} {E Ê} {e ê} {I Î}
                                 {i î} {O Ô} {o ô} {U Û} {u Û}}
	           {asciicircum ^ {A Â} {a â} {E Ê} {e ê} {I Î}
                                 {i î} {O Ô} {o ô} {U Û} {u Û}}
		   {grave ` {A À} {a à} {E È} {e è} {I Ì} {i ì} {O Ò} {o Ò}
                            {U Ù} {u ù}}
	           {dead_grave ` {A À} {a à} {E È} {e è} {I Ì} {i ì}
		            {O Ò} {o Ò} {U Ù} {u ù}}
		   {quotedbl \\\" {a ä} {e ë} {i ï} {o ö} {u ü} {s ß}
                                {quotedbl ¨}
  		                {A Ä} {E Ë} {I Ï} {O Ö} {U Ü}}
		   {slash / {a å} {e æ} {o ø} {A Å} {E Æ} {O Ø}}
			   }
    }
    foreach k $binds {
	set keysym [lindex $k 0]
	bind $w <Key-$keysym> "$w insert insert [lindex $k 1] ; break"
	foreach p [lrange $k 2 end] {
	    bind $w <Key-$keysym><Key-[lindex $p 0]> \
		    "ReplaceChar $w [lindex $p 1] ; break"
	}
    }
    return
}

proc ReplaceChar {w char} {
    # replace last char in entry or text widget $w by $char

    switch [winfo class $w] {
	Entry {
	    if { [set ix [expr [$w index insert]-1]] < 0 } { return }
	    $w delete $ix
	    $w insert insert $char
	}
	Text {
	    if { ! [regexp {^([0-9]+)\.([0-9]+)$} [$w index insert] a l c] || \
		$c == 0 } {
		return
	    }
	    $w delete $l.[expr $c-1]
	    $w insert insert $char	    
	}
    }
    return
}




