;;;;
;;;; Entries bindings and procs
;;;;
;;;; Copyright (C) 1993,1994,1995 Erick Gallesio - I3S-CNRS/ESSI <eg@unice.fr>
;;;; 
;;;; Permission to use, copy, and/or distribute this software and its
;;;; documentation for any purpose and without fee is hereby granted, provided
;;;; that both the above copyright notice and this permission notice appear in
;;;; all copies and derived works.  Fees for distribution or use of this
;;;; software or derived works may only be charged with express written
;;;; permission of the copyright holder.  
;;;; This software is provided ``as is'' without express or implied warranty.
;;;;
;;;; This software is a derivative work of other copyrighted softwares; the
;;;; copyright notices of these softwares are placed in the file COPYRIGHTS
;;;;
;;;;
;;;;           Author: Erick Gallesio [eg@unice.fr]
;;;;    Creation date: 17-May-1993 12:35
;;;; Last file update:  2-Jun-1994 12:42
;;;;

;; ----------------------------------------------------------------------
;; Class bindings for entry widgets.
;; ----------------------------------------------------------------------

;; Button 1 bindings

(do-bindings "Entry"  '(
  ;; Button 1 bindings
  ("<1>"		. (begin
			    (|%W| 'icursor '@%x)
			    (|%W| 'select 'from '@%x)
			    (when(equal? (tk-get |%W| :state) "normal") 
				 (focus |%W|))))
  ("<B1-Motion>"	. (|%W| 'select 'to '@%x))

  ;; Button 2 bindings
  ("<2>"		. (catch 
			   [|%W| 'insert 'insert (selection 'get)]
			   [tk-entrySeeCaret |%W|]))
  ("<Shift-2>"		. (|%W| 'scan 'mark %x))
  ("<Shift-B2-Motion>"	. (|%W| 'scan 'dragto %x))

  ;; Button 3 bindings
  ("<3>"		. (|%W| 'select 'adjust '@%x))
  
  ;; Special keys bindings
  ("<Control-a>"	. (begin 
			    (|%W| 'icursor 0) 
			    (tk-entrySeeCaret |%W|)))
  ("<Control-b>"	. (tk-backwardChar |%W|))
  ("<Control-c>"	. (|%W| 'delete 0 'end))
  ("<Control-d>"	. (begin 
			    (|%W| 'delete 'insert)
			    (tk-entrySeeCaret |%W|)))
  ("<Control-e>"	. (begin 
			    (|%W| 'icursor 'end)
			    (tk-entrySeeCaret |%W|)))
  ("<Control-f>"	. (tk-forwardChar |%W|))
  ("<Control-g>"	. (|%W| 'delete 0 'end))
  ("<Control-h>"	. (tk-entryBackspace |%W|))
  ("<Control-k>"	. (begin 
			    (|%W| 'delete 'insert 'end)
			    (tk-entrySeeCaret |%W|)))
  ("<Control-w>"	. (catch
			    [set! tk::kill-buffer
				  (|%W| 'delete 'sel.first 'sel.last)]
			    [|%W| 'delete 'sel.first 'sel.last]
			    [tk-entrySeeCaret |%W|]))
  ("<Control-y>"	. (begin
			    (|%W| 'insert 'insert tk::kill-buffer)
			    (tk-entrySeeCaret |%W|)))
  ("<Delete>" 		. (tk-entryBackspace |%W|))
  ("<BackSpace>"	. (tk-entryBackspace |%W|))
  ("<Any-backslash>"	. (begin
			    (|%W| 'insert 'insert "\\")
			    (tk-entrySeeCaret |%W|)))
  ("<Any-quotedbl>"	. (begin
			    (|%W| 'insert 'insert "\"")
			    (tk-entrySeeCaret |%W|)))
  ("<Any-KeyPress>"	. (unless (equal? "%A" "")
			    (|%W| 'insert 'insert "%A")
			    (tk-entrySeeCaret |%W|)))
))


;; Entries utility functions

(define (tk-entryIndex w pos)
  (w 'index pos))

(define (tk-forwardChar w)
  (w 'icursor (+ (tk-entryIndex w 'insert) 1))
  (tk-entrySeeCaret w))

(define (tk-backwardChar w)
  (w 'icursor (- (tk-entryIndex w 'insert) 1))
  (tk-entrySeeCaret w))

(define (tk-entryBackspace w)
  (let ((x  (- (tk-entryIndex w 'insert) 1)))
    (if (>= x 0) (begin (w 'delete x) (tk-entrySeeCaret w)))))

;; The procedure below is invoked after insertions.  If the caret is not
;; visible in the window then the procedure adjusts the entry's view to
;; bring the caret back into the window again.

(define (tk-entrySeeCaret w)
  (let ((c     (tk-entryIndex w 'insert))
	(left  (tk-entryIndex w "@0"))
	(width (format #f "@~A" (- (winfo 'width w) 5))))

    (when (>= left c) 
      (w 'view (- c [if (> c 0) 1 0 ])))
      
    (do ((right (tk-entryIndex w width) (tk-entryIndex w width)))
	((or (>= right c) (>= left c)))
      (set! left (+ left 1))
      (w 'view left))))

;;;;;;;
;;tk-bindForTraversal Entry
;;;;;;;
