e$B1J0f!wCNG=!%6e9)Bg$G$9!%e(B
From: SASADA Koichi [email protected]
Subject: [ruby-dev:32399] Re: Proc e$BFb$Ne(B self e$B$NCM$,2=$1$k!)e(B
Date: Thu, 29 Nov 2007 19:42:13 +0900
Message-ID: [email protected]
Hidetoshi NAGAI wrote:
e$BH/@8>r7o$,$h$/J,$+$i$J$$$N$G$9$,!$e(BProc e$B%*%V%8%'%/%HFbIt$Ne(B self e$B$NCM$,e(B
e$B$$$D$N4V$K$+2=$1$F$7$^$&8=>]$,=P$F$7$^$$!$:$$C$F$$$^$9!%e(B
e$B!!$&!<$s!%IT;W5D!%!V2?$i$+$N=hM}!W$,$o$+$i$J$$$H%G%P%C%0$N$7$h$&$,L5e(B
e$B$$46$8$G$9$M$'!%e(BGC e$B$N%^!<%/O3$l$+$b$7$l$^$;$s!%e(B
e$B$=$l$O$=$&$G$9$h$M$‘!%!%!%e(B
e$B$H$j$"$($:!$8=>]$r3NG’$G$-$k$+$b$7$l$J$$%9%/%j%W%H$rKvHx$KE:IU$7$^$9!%e(B
e$B8=:_$Ne(B trunk e$B$Ne(B Ruby/Tk
e$B$O?‘!9$J0UL#$G$^$H$b$K$OF0$-$^$;$s$,!$e(B
e$B$3$N%9%/%j%W%HDxEY$O$J$s$H$+F0$/$O$:$G$9!%e(B
e$B%^%&%9%+!<%=%k$r%&%#%8%’%C%H>e$K;}$C$F$$$/$H%3!<%k%P%C%/$,8F$P$l!$e(B
e$B@5>o$J7k2L$d2=$1$?7k2L$r=PNO$7$?$j!$e(BSEGV
e$B$r@8$8$?$j$9$k$H;W$$$^$9!%e(B
----<e$B$3$3$h$jM>CLe(B>--------------------------------------------------
Ruby/Tk e$B$re(B 1.9
e$B$K$-$A$s$HBP1~$5$;$k$K$OBgI}$JJQ99$,I,MW$G$9!%e(B
e$B<BAu$NJ}?K$O$[$\7h$^$C$?$N$G$9$,!$e(B12
e$B7n$N%j%j!<%9$K4V$K9g$o$;$k$N$Oe(B
e$B$+$J$jFq$7$$$H;W$C$F$^$9!%e(B
e$B$=$3$Ge(B 12 e$B7n$N%j%j!<%9$K$O!$8=>u$N$b$N$K<j$rF~$l$Fe(B
e$B$"$kDxEY$^$G$OF0$/$b$N$K$9$k$H$$$&7A$r9M$($F$$$^$9!%e(B
e$B$=$N>e$G!$$-$A$s$HBP1~$7$?$b$N$r:n$C$F$$$/$D$b$j$G$9!%e(B
e$B@5<0BP1~HG$G$O!$e(BTk e$B$N%3%"ItJ,$OFHN)$7$?e(B native thread
e$B$GF0$-!$e(B
Ruby e$B$Ne(B thread switching e$B$de(B GC
e$B$KHQ$o$5$l$J$$$b$N$K$J$kM=Dj$G$9!%e(B
Ruby VM e$B$,e(B model 3 e$B$K$J$C$?>l9g$G$b$=$N$^$^$GBg>fIW$J$b$N$Ke(B
e$B$J$k$@$m$&$H;W$C$F$^$9!%e(B
e$B$A$J$_$Ke(B 12 e$B7nCf=$K$Oe(B Tcl/Tk 8.5 e$B$N&B$,<h$l$Fe(B
e$B@5<0HG$,%j%j!<%9$5$l$k$H$$$&OC$b$"$k$h$&$G$9$+$i!$e(B
e$B$3$l$K$-$A$s$HBP1~$7$?$b$N$K$7$?$$$H$b;W$C$F$^$9!%e(B
----<e$B$3$3$^$GM>CLe(B>--------------------------------------------------
e$B%9%/%j%W%H$K$O%4%_$N$h$&$J=PNO$r$9$k9T$,4^$^$l$F$$$^$9$,!$e(B
e$B$&$A$N4D6-$G$O$=$N9T$r:o$k$HB(:B$Ke(B SEGV
e$B$7$F$7$^$&$N$G;D$7$F$^$9!%e(B
e$B4D6-$,0c$($PA4$/0c$C$?F0:n$K$J$C$F$7$^$&2DG=@-$,$"$j$^$9!%e(B
e$B$&$A$N4D6-$GE:IU$N%9%/%j%W%H$rF0:n$5$;$k$He(B
±------------+
| button-1 |
±------------+
| button-2 |
±------------+
| listbox-1 |
| |
±------------+
| listbox-2 |
| |
±------------+
e$B$H$$$&$h$&$KI=<($5$l!$3F%&%#%8%'%C%H$K%^%&%9%+!<%=%k$,F~$k$He(B
button-1 → SEGV
button-2 → e$B0[>oe(B: self e$B$Oe(B “eql?” e$B$H$$$&e(B String
e$B%%V%8%'%/%He(B
listbox-1 → e$B@5>oe(B: self e$B$Oe(B TkBalloonHelp
e$B%%V%8%‘%/%He(B
listbox-2 → e$B@5>oe(B: self e$B$Oe(B TkBalloonHelp
e$B%*%V%8%’%/%He(B
e$B$H$$$&$h$&$J=PNO$,$J$5$l$^$9!%e(B
e$B%9%/%j%W%H$r>/$7$$$8$k$He(B listbox e$B$G$be(B SEGV
e$B$9$k$h$&$K$J$C$?$j!$e(B
button-2 e$B$,JL$NJ8;zNse(B (e$BNc$($Pe(B “===”)
e$B$K2=$1$k$h$&$K$J$C$?$je(B
e$B$H$$$&>u67$,@8$8$^$9!%e(B
e$B0lBNA4BN2?$,$I$&$J$C$F$$$k$d$i$H$$$&46$8$G$9!%e(B
e$B$H$3$m$G!$e(BProc#binding e$B$C$F$J$/$J$C$?$s$G$9$+!)e(B
e$B:#2s$N8=>]$rD4$Y$k:]$K;H$*$&$H$7$?$iB8:_$7$J$+$C$?$b$N$G!%e(B
e$B!!$O$$!%$"$C$?$[$&$,$$$$$G$9$+$M!)e(B
e$B>/$J$/$H$b:#2s$O$=$l$,$“$C$?J}$,$&$l$7$+$C$?$o$1$G$9$,!$e(B
e$B<B:]$N$H$3$m!$$I$NDxEY$N<{MW$,$”$k$N$+$O$o$+$j$^$;$s!%e(B
e$B$I$N$h$&$J5DO@$N7k2L$G$=$&$J$C$?$N$+$OCN$i$J$$$N$G$9$,!$e(B
e$B<BAu$,Fq$7$+$C$?$j%;%-%e%j%F%#>e$N7j$K$J$C$?$j$H$+$NLdBj$G$7$g$&$+!)e(B
e$BFC$KLdBj$,$J$$$N$G$“$l$P$H$$$&>r7o2<$G!$e(B
e$B$”$C$?J}$,JQ$J$3$He(B(^_^)e$B$,$G$-$F3Z$7$$$+$b$7$l$^$;$s!%e(B
------------<e$B$3$3$+$i%9%/%j%W%He(B>-----------------------------------
require ‘tk’
class TkBalloonHelp<TkLabel
@@count = 0
def _balloon_binding(interval)
@timer = TkAfter.new(interval, 1, proc{show})
def @timer.interval(val)
@sleep_time = val
end
@bindtag = TkBindTag.new
@bindtag.bind(‘Enter’, proc{@timer.start})
x = binding()
cnt = (@@count += 1)
p [:init, cnt, self.class, eval('self.class', x), self]
@bindtag.bind('Enter', proc{
p [:callback, cnt, self.class, eval('self.class',
x), self]
})
@bindtag.bind(‘Motion’, proc{@timer.restart; erase})
@bindtag.bind(‘Any-ButtonPress’, proc{@timer.restart; erase})
@bindtag.bind(‘Leave’, proc{@timer.stop; erase})
tags = @parent.bindtags
idx = tags.index(@parent)
unless idx
ppath = TkComm.window(@parent.path)
idx = tags.index(ppath) || 0
end
tags[idx,0] = @bindtag
@parent.bindtags(tags)
end
private :_balloon_binding
def initialize(parent=nil, keys={})
@parent = parent || Tk.root
@frame = TkToplevel.new(@parent)
@frame.withdraw
@frame.overrideredirect(true)
@frame.transient(TkWinfo.toplevel(@parent))
@epath = @frame.path
if keys
keys = _symbolkey2str(keys)
else
keys = {}
end
@command = keys.delete('command')
@interval = keys.delete('interval'){1000}
_balloon_binding(@interval)
@label = TkLabel.new(@frame, 'background'=>'bisque').pack
@label.configure(_symbolkey2str(keys)) unless keys.empty?
@path = @label
end
def epath
@epath
end
def interval(val)
if val
@timer.interval(val)
else
@interval
end
end
def command(cmd = Proc.new)
@command = cmd
self
end
def show
x = TkWinfo.pointerx(@parent)
y = TkWinfo.pointery(@parent)
@frame.geometry(“+#{x+1}+#{y+1}”)
if @command
case @command.arity
when 0
@command.call
when 2
@command.call(x - TkWinfo.rootx(@parent), y -
TkWinfo.rooty(@parent))
when 3
@command.call(x - TkWinfo.rootx(@parent), y -
TkWinfo.rooty(@parent),
self)
else
@command.call(x - TkWinfo.rootx(@parent), y -
TkWinfo.rooty(@parent),
self, @parent)
end
end
@frame.deiconify
@frame.raise
@org_cursor = @parent['cursor']
@parent.cursor('crosshair')
end
def erase
@parent.cursor(@org_cursor)
@frame.withdraw
end
def destroy
@frame.destroy
end
end
################################################
test
################################################
if FILE == $0
TkButton.new(‘text’=>‘This button has a balloon help’) {|b|
pack(‘fill’=>‘x’)
TkBalloonHelp.new(b, ‘text’=>’ Message ')
}
x = TkButton.new(‘text’=>‘This button has another balloon help’) {|b|
pack(‘fill’=>‘x’)
TkBalloonHelp.new(b, ‘text’=>‘configured message’,
‘interval’=>200, ‘font’=>‘courier’,
‘background’=>‘gray’, ‘foreground’=>‘red’)
}
puts ‘1 ---------------------------------------------------------’
p tag = x.bindtags[0]
p tag.methods.sort
p cmd_id = Tk.tk_call(‘bind’, tag.to_eval, ‘’).split(’ ‘)[1]
p TkCore::INTERP.tk_cmd_tbl[cmd_id]
p cmd_proc =
TkCore::INTERP.tk_cmd_tbl[cmd_id].instance_variable_get(’@cmd’)
cmd_proc.call
puts ‘1 **********************************************************’
sb = TkScrollbox.new.pack(:fill=>:x)
sb.insert(:end, *%w(aaa bbb ccc ddd eee fff ggg hhh iii jjj kkk lll
mmm))
puts ‘2 ---------------------------------------------------------’
p tag = x.bindtags[0]
p tag.methods.sort
p cmd_id = Tk.tk_call(‘bind’, tag.to_eval, ‘’).split(’ ‘)[1]
p TkCore::INTERP.tk_cmd_tbl[cmd_id]
p cmd_proc =
TkCore::INTERP.tk_cmd_tbl[cmd_id].instance_variable_get(’@cmd’)
cmd_proc.call
puts ‘2 **********************************************************’
CASE4b : command is a Method object and takes 4 arguemnts
def set_msg(x, y, bhelp, parent)
bhelp.text “current index == #{parent.nearest(y)}”
end
cmd = self.method(:set_msg)
puts ‘3 ---------------------------------------------------------’
p tag = x.bindtags[0]
p tag.methods.sort
p cmd_id = Tk.tk_call(‘bind’, tag.to_eval, ‘’).split(’ ‘)[1]
p TkCore::INTERP.tk_cmd_tbl[cmd_id]
p cmd_proc =
TkCore::INTERP.tk_cmd_tbl[cmd_id].instance_variable_get(’@cmd’)
cmd_proc.call
puts ‘3 **********************************************************’
TkBalloonHelp.new(sb, :interval=>500,
:relief=>:ridge, :background=>‘white’,
:command=>cmd)
sb2 = TkScrollbox.new.pack(:fill=>:x)
sb2.insert(:end, *%w(AAA BBB CCC DDD EEE FFF GGG HHH III JJJ KKK LLL
MMM))
TkBalloonHelp.new(sb2, :interval=>500,
:padx=>5, :relief=>:raised,
:background=>‘gray25’, :foreground=>‘white’,
:command=>cmd)
Tk.mainloop
end
------------<e$B$3$3$^$G%9%/%j%W%He(B>-----------------------------------