Version 0 of widget:repeaterButton

Updated 2002-03-11 06:38:06

GPS - Sun Mar 10, 2002: I looked at some code to a button that repeated an action and found it to be overly complex, so that inspired me to whip up this concise little tool.


  #!/bin/wish8.3

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

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

  proc widget:repeaterButton:ButtonPress-1 {win} {
    eval [$win cget -command]
    if {$::buttonPressed == 1} {
      $win configure -relief sunken
      after 20 "widget:repeaterButton:ButtonPress-1 $win"
    }
  }

  proc widget:repeaterButton:ButtonRelease-1 {win} {
    $win configure -relief raised
  }

  proc widget:repeaterButton {win args} {
    bind:copyClass Button RepeaterButton

    bind RepeaterButton <ButtonPress-1> "set ::buttonPressed 1; \
      widget:repeaterButton:ButtonPress-1 %W"
    bind RepeaterButton <ButtonRelease-1> "set ::buttonPressed 0; \
      widget:repeaterButton:ButtonRelease-1 %W"

    eval button $win $args

    bindtags $win "RepeaterButton all"
    return $win
  }


  #test code
  catch {console show}
  pack [widget:repeaterButton .rb -text "Say Hello" -command "puts HELLO"]