Re: how to add a binding to a widget ?

From: Erick Gallesio <eg_at_kaolin.unice.fr>
Date: Fri, 07 Apr 1995 10:15:11 +0100

> Fred Xia writes
>
> I'm new to STK. I wonder if it is possible to add a binding
> to a widget, similar to the Tk's {+ ...} syntax, without losing the
> existing bindings.
>

Using '+' will make a buggy binding and I should correct that (but binding
have changed a lot in Tk4.0, so I will wait until porting it).

However following code is bettre imho than the Tcl/Tk '+' since it permits to
add code before or after a binding and since it retrieves the class binding if
the widget doesn't overload it (which is not the case with Tcl).
Following code works with pure Tk and STklos.

(define (add-binding widget event binding where)
  (let ((old (bind widget event)))
    (when (null? old)
       ;; Try to look if a class binding already exists for this event
       (set! old (bind (winfo 'class widget) event)))
    (bind widget
          event
          (if (null? old)
              binding
              (if (eqv? where :before)
                  `(begin ,binding ,old)
                  `(begin ,old ,binding))))))

For instance, you can do:

        (define b (make <Button> :text "Hello" :command '(display "Hello\n")))
        (pack b)
        (add-binding b "<ButtonRelease-1>" '(display "Before\n") :before)
        (add-binding b "<ButtonRelease-1>" '(display "After\n") :after)

and when Clicking on this button you will have the 3 messages printed.

Perhaps, I should add this procedure to the STk library.


                -- Erick
Received on Fri Apr 07 1995 - 10:15:12 CEST

This archive was generated by hypermail 2.3.0 : Mon Jul 21 2014 - 19:38:59 CEST