Version 2 of Inheriting Widget Binding Classes

Updated 2002-02-08 11:19:53

Thu Jan 31 12:16:22 MST 2002 - GPS: I looked at the various ways in which people modify bindings to make a read only text widget and came up with the code below. The nice thing about this vs. the Read-only text widget page is that it doesn't require a modified file -- it does it all in memory.


  #!/bin/wish8.3

  proc copyBindingClass {class newClass} {
        set bindingList [bind $class]
        #puts $bindingList

        foreach binding $bindingList {
                bind $newClass $binding [bind $class $binding]
        }
  }

  proc removeBindingsFromClass {class removeList} {
        foreach item $removeList {
                bind $class <$item> {}
        }
  }

  pack [text .t]
  .t insert end "Hello World"

  copyBindingClass Text Text.t

  bindtags .t ".t Text.t all"

  removeBindingsFromClass Text.t [list KeyPress Delete BackSpace Meta-Key-Delete \
        Key-Tab Key-Insert Key-Left Key-Right Key-Up Key-Down Shift-Key-Left \
        Shift-Key-Right Shift-Key-Up Shift-Key-Down Control-Key-Left Control-Key-Right \
        Control-Key-Up Control-Key-Down Control-Shift-Key-Left Control-Shift-Key-Right \
        Key-Return Key-BackSpace Control-Key-space Control-Shift-Key-space \
        Shift-Key-Select <Paste> <Cut> <Clear>]

BBH 8Feb2002 - I like this approach (saves the extra file) - here is a variant that I created so I don't have to list out all the bindings that alter a text widget - just examine the bindings themselves!

 # generate bindings for ROText based on Text bindings
 # this exp finds anything that modifies a text widget contents
 set exp  {(%W (insert|delete))|((tkText|tk_text)(Insert|Paste|Cut))}
 foreach ev [bind Text] {
     set body [bind Text $ev]
     if {![regexp $exp $body]} {
         bind ROText $ev $body
     }
 }
 # define new proc for creating read only text widgets
 proc rotext {args} {
     set w [eval [linsert $args 0 text]]
     bindtags $w "$w ROText [winfo toplevel $w] all"
     return $w
 }