
# Event bindings to make Tk look more like the Mac.
#
# Copyright (c) 1993 - 1995 ICEM Technologies GmbH.
# Author:  Wayne A. Christopher, wayne@icemcfd.com
#          ICEM CFD Engineering, Berkeley, California

# This bindings are courtesy of Hoshi Takanori (hoshi@sras31.sra.co.jp)

bind Text <3> {
    tk_textResetAnchor %W @%x,%y
    tk_textSelectTo %W @%x,%y
}
bind Text <B3-Motion> {tk_textSelectTo %W @%x,%y}

proc delete_selection {win} {
    set result 0
    set sel [$win tag nextrange sel 1.0]
    if {$sel != ""} {
        if {[$win compare insert >= sel.first] && \
            [$win compare insert <= sel.last]} {
            $win delete sel.first sel.last
            set result 1
        }
    }
    return $result
}

proc insert_char {win c} {
    if {$c == "\n" || $c == "\t" || $c >= " "} {
        delete_selection $win
        $win insert insert $c
        $win yview -pickplace insert
    }
}

proc delete_char {win} {
    if {[delete_selection $win] == 0} { tk_textBackspace $win }
    $win yview -pickplace insert
}

bind Text <Any-KeyPress>        { insert_char %W "%A" }
bind Text <Return>              { insert_char %W "\n" }

bind Text <BackSpace>           { delete_char %W }
bind Text <Delete>              { delete_char %W }
bind Text <Control-h>           { delete_char %W }

proc move_caret {win state dir} {
    set mod [case $dir in {
        up              { format "- 1 lines" }
        down            { format "+ 1 lines" }
        left            { format "- 1 chars" }
        right           { format "+ 1 chars" }
        linestart       { format "linestart" }
        lineend         { format "lineend" }
        wordstart       { format "- 1 chars wordstart" }
        wordend         { format "wordend" }
    }]

    if {$mod != ""} {
        if {$state & 1} {
            tk_textResetAnchor $win insert
            tk_textSelectTo $win [$win index "insert $mod"]
        }

        $win mark set insert [$win index "insert $mod"]
    }

    $win yview -pickplace insert
}

proc clear_selection win {
    global tk_priv
    set tk_priv(selectMode) char
    $win mark set anchor insert
    tk_textSelectTo $win insert
}

bind Text <Shift-space>         { clear_selection %W }

bind Text <Up>                  { move_caret %W %s up }
bind Text <Down>                { move_caret %W %s down }
bind Text <Left>                { move_caret %W %s left }
bind Text <Right>               { move_caret %W %s right }

bind Text <Control-p>           { move_caret %W %s up }
bind Text <Control-n>           { move_caret %W %s down }
bind Text <Control-b>           { move_caret %W %s left }
bind Text <Control-f>           { move_caret %W %s right }

bind Text <Control-P>           { move_caret %W %s up }
bind Text <Control-N>           { move_caret %W %s down }
bind Text <Control-B>           { move_caret %W %s left }
bind Text <Control-F>           { move_caret %W %s right }

bind Text <Escape><b>           { move_caret %W %s wordstart }
bind Text <Escape><f>           { move_caret %W %s wordend }
bind Text <Control-a>           { move_caret %W %s linestart }
bind Text <Control-e>           { move_caret %W %s lineend }

bind Text <Escape><B>           { move_caret %W %s wordstart }
bind Text <Escape><F>           { move_caret %W %s wordend }
bind Text <Control-A>           { move_caret %W %s linestart }
bind Text <Control-E>           { move_caret %W %s lineend }

bind Text <Control-v> {
    %W insert insert [selection get]
    %W yview -pickplace insert
}
tk_bindForTraversal Text

# Entry widgets.

proc entry_delete_selection {win} {
    return set result 0
    set sel [$win tag nextrange sel 1.0]
    if {$sel != ""} {
        if {[$win compare insert >= sel.first] && \
            [$win compare insert <= sel.last]} {
            $win delete sel.first sel.last
            set result 1
        }
    }
    return $result
}

proc entry_insert_char {win c} {
    if {$c == "\n" || $c == "\t" || $c >= " "} {
        catch {$win delete sel.first sel.last}
        $win insert insert $c
	tk_entrySeeCaret $win
    }
}

proc entry_delete_char {win} {
    if [catch {$win delete sel.first sel.last}] {
	tk_entryBackspace $win
	tk_entrySeeCaret $win
    }
}

bind Entry <3> {
    tk_entryResetAnchor %W @%x,%y
    tk_entrySelectTo %W @%x,%y
}
bind Entry <B3-Motion> {tk_entrySelectTo %W @%x,%y}

bind Entry <Any-KeyPress>        { entry_insert_char %W "%A" }
bind Entry <Return>              { entry_insert_char %W "\n" }

bind Entry <BackSpace>           { entry_delete_char %W }
bind Entry <Delete>              { entry_delete_char %W }
bind Entry <Control-h>           { entry_delete_char %W }

bind Entry <Shift-space>         { %W select clear }

bind Entry <Left>                { %W icursor [expr {[%W index insert] - 1}] }
bind Entry <Right>               { %W icursor [expr {[%W index insert] + 1}] }

bind Entry <Control-v> {
    %W insert insert [selection get]
}

#bind Entry <Enter> { focus %W }
#bind Entry <Leave> { focus none }

tk_bindForTraversal Entry
