1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* ext/tk/sample/tkextlib/tile/demo.rb: added repeating buttons demo.

* ext/tk/sample/tkextlib/tile/repeater.tcl: ditto. (new file)


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@8886 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
ocean 2005-08-02 06:51:26 +00:00
parent 91d7a02959
commit 909e63ac3c
3 changed files with 159 additions and 8 deletions

View file

@ -1,3 +1,9 @@
2005-08-01 ocean <ocean@ruby-lang.org>
* sample/tkextlib/tile/demo.rb: added repeating buttons demo.
* sample/tkextlib/tile/repeater.tcl: ditto. (new file)
2005-08-01 ocean <ocean@ruby-lang.org> 2005-08-01 ocean <ocean@ruby-lang.org>
* lib/tkextlib/tile.rb: fixed autoload for Treeview. * lib/tkextlib/tile.rb: fixed autoload for Treeview.
@ -5,8 +11,8 @@
* lib/tkextlib/tile/treeview.rb: replaced `ary2tk_list(items)' with * lib/tkextlib/tile/treeview.rb: replaced `ary2tk_list(items)' with
`*items'. `*items'.
* sample/tkextlib/tile: added treeview demo. (tile 0.5 or later is * sample/tkextlib/tile/demo.rb: added treeview demo. (tile 0.5 or
needed) [ruby-dev:26668] later is required) [ruby-dev:26668]
2005-08-01 ocean <ocean@ruby-lang.org> 2005-08-01 ocean <ocean@ruby-lang.org>

View file

@ -10,6 +10,7 @@ Tk::AUTO_PATH.lappend('.', demodir, File.join(demodir, 'themes'))
require 'tkextlib/tile' require 'tkextlib/tile'
Tk.load_tclscript(File.join(demodir, 'toolbutton.tcl')) Tk.load_tclscript(File.join(demodir, 'toolbutton.tcl'))
Tk.load_tclscript(File.join(demodir, 'repeater.tcl'))
# This forces an update of the available packages list. It's required # This forces an update of the available packages list. It's required
# for package names to find the themes in demos/themes/*.tcl # for package names to find the themes in demos/themes/*.tcl
@ -307,10 +308,6 @@ def makeNotebook
nb.add(tree, :text=>'Tree') nb.add(tree, :text=>'Tree')
others = Tk::Tile::TFrame.new(nb) others = Tk::Tile::TFrame.new(nb)
nb.add(others, :text=>'Others', :underline=>4) nb.add(others, :text=>'Others', :underline=>4)
nb.add(Tk::Tile::TLabel.new(nb, :text=>'Nothing to see here...'),
:text=>'Stuff', :sticky=>:new)
nb.add(Tk::Tile::TLabel.new(nb, :text=>'Nothing to see here either.'),
:text=>'More Stuff', :sticky=>:se)
[nb, client, combo, tree, others] [nb, client, combo, tree, others]
end end
@ -624,7 +621,11 @@ showDescription.bind('Leave', proc{|w| msg.text('')}, '%W')
"Shows how Tile and standard scrollbars differ when they're sized too large" ], "Shows how Tile and standard scrollbars differ when they're sized too large" ],
[ :trackFocus, "Track keyboard focus..." , [ :trackFocus, "Track keyboard focus..." ,
"Display the name of the widget that currently has focus" ] "Display the name of the widget that currently has focus" ],
[ :repeatDemo, "Repeating buttons...",
"Demonstrates custom classes (see demos/repeater.tcl)" ]
].each{|demo_cmd, label, description| ].each{|demo_cmd, label, description|
b = Tk::Tile::TButton.new(others, :text=>label, b = Tk::Tile::TButton.new(others, :text=>label,
:command=>proc{ self.__send__(demo_cmd) }) :command=>proc{ self.__send__(demo_cmd) })
@ -722,7 +723,6 @@ end
# #
# Widget state demo: # Widget state demo:
# #
$Widget = TkVariable.new $Widget = TkVariable.new
TkBindTag::ALL.bind('Control-Shift-ButtonPress-1', TkBindTag::ALL.bind('Control-Shift-ButtonPress-1',
@ -808,4 +808,32 @@ def changeState(st)
end end
end end
#
# Repeating buttons demo:
#
def repeatDemo
if defined?($repeatDemo) && $repeatDemo.exist?
$repeatDemo.deiconify; return
end
$repeatDemo = TkToplevel.new(:title=>'Repeating button')
f = Tk::Tile::TFrame.new($repeatDemo)
b = Tk::Tile::TButton.new(f, :class=>'Repeater', :text=>'Press and hold')
begin
p = Tk::Tile::TProgressbar.new(f, :orient=>:horizontal, :maximum=>10)
rescue # progressbar is not supported (tile 0.4)
p = Tk::Tile::TLabel.new(f, :text=>0)
def p.step
i = self.text.to_i + 1
i = 0 if i >= 10
self.text(i.to_s)
end
end
b.command {p.step}
b.pack(:side=>:left, :expand=>false, :fill=>:none, :padx=>6, :pady=>6)
p.pack(:side=>:right, :expand=>true, :fill=>:x, :padx=>6, :pady=>6)
f.pack(:expand=>true, :fill=>:both)
end
Tk.mainloop Tk.mainloop

View file

@ -0,0 +1,117 @@
#
# $Id$
#
# Demonstration of custom classes.
#
# The Tile button doesn't have built-in support for autorepeat.
# Instead of adding -repeatdelay and -repeatinterval options,
# and all the extra binding scripts required to deal with them,
# we create a custom widget class for autorepeating buttons.
#
# Usage:
# ttk::button .b -class Repeater [... other options ...]
#
# TODO:
# Use system settings for repeat interval and initial delay.
#
# Notes:
# Repeater buttons work more like scrollbar arrows than
# Tk repeating buttons: they fire once immediately when
# first pressed, and $State(delay) specifies the initial
# interval before the button starts autorepeating.
#
namespace eval tile::Repeater {
variable State
set State(timer) {} ;# [after] id of repeat script
set State(interval) 100 ;# interval between repetitions
set State(delay) 300 ;# delay after initial invocation
}
### Class bindings.
#
bind Repeater <Enter> { %W state active }
bind Repeater <Leave> { %W state !active }
bind Repeater <Key-space> { tile::Repeater::Activate %W }
bind Repeater <<Invoke>> { tile::Repeater::Activate %W }
bind Repeater <ButtonPress-1> { tile::Repeater::Press %W }
bind Repeater <ButtonRelease-1> { tile::Repeater::Release %W }
bind Repeater <B1-Leave> { tile::Repeater::Pause %W }
bind Repeater <B1-Enter> { tile::Repeater::Resume %W } ;# @@@ see below
# @@@ Workaround for metacity-induced bug:
bind Repeater <B1-Enter> \
{ if {"%d" ne "NotifyUngrab"} { tile::Repeater::Resume %W } }
### Binding procedures.
#
## Activate -- Keyboard activation binding.
# Simulate clicking the button, and invoke the command once.
#
proc tile::Repeater::Activate {w} {
$w instate disabled { return }
set oldState [$w state pressed]
update idletasks; after 100
$w state $oldState
after idle [list $w invoke]
}
## Press -- ButtonPress-1 binding.
# Invoke the command once and start autorepeating after
# $State(delay) milliseconds.
#
proc tile::Repeater::Press {w} {
variable State
$w instate disabled { return }
$w state pressed
$w invoke
after cancel $State(timer)
set State(timer) [after $State(delay) [list tile::Repeater::Repeat $w]]
}
## Release -- ButtonRelease binding.
# Stop repeating.
#
proc tile::Repeater::Release {w} {
variable State
$w state !pressed
after cancel $State(timer)
}
## Pause -- B1-Leave binding
# Temporarily suspend autorepeat.
#
proc tile::Repeater::Pause {w} {
variable State
$w state !pressed
after cancel $State(timer)
}
## Resume -- B1-Enter binding
# Resume autorepeat.
#
proc tile::Repeater::Resume {w} {
variable State
$w instate disabled { return }
$w state pressed
$w invoke
after cancel $State(timer)
set State(timer) [after $State(interval) [list tile::Repeater::Repeat $w]]
}
## Repeat -- Timer script
# Invoke the command and reschedule another repetition
# after $State(interval) milliseconds.
#
proc tile::Repeater::Repeat {w} {
variable State
$w instate disabled { return }
$w invoke
set State(timer) [after $State(interval) [list tile::Repeater::Repeat $w]]
}
#*EOF*