···
From: "Phlip" <phlip_cpp@yahoo.com>
Subject: Re: Fake Tk events
Date: Wed, 18 Aug 2004 15:40:56 +0900
Message-ID: <wQCUc.3907$Y94.3738@newssvr33.news.prodigy.com>
BTW 3 Tk books at Fry's had NOTHING about event generation, send, etc.
One of the examples of remote control with Tcl's send command
is 'ext/tk/lib/remote-tk.rb' (included in 1.8.2 preview).
The library can create a remote interpreter object to control
a Tk interpreter on the other process.
'ext/tk/sample/remote-ip_sample*.rb' are samples of the library.
About 'event generation', I wrote an example in my Ruby/Tk book
(Japanese). The following is the example (but a little changed).
It is also an example of 'Tk.callback_continue/callback_break',
'bindtag' and 'virtual event'.
--------------------------------------------------------
require 'tk'
root = TkRoot.new{ title('Hello Message') }
v = TkVariable.new('someone')
f1 = TkFrame.new.pack('side'=>'top', 'anchor'=>'w')
TkLabel.new(f1, 'text'=>'Input Your Name : ',
'font'=>'times').pack('side'=>'left')
e = TkEntry.new(f1, 'textvariable'=>v).pack('side'=>'left')
asc_only = TkBindTag.new
asc_only.bind('Key', proc{|a|
if a.kind_of?(String) &&
( a == ' ' || a[0] < 0x20 ||
(a[0] >= ?a && a[0] <= ?z) ||
(a[0] >= ?A && a[0] <= ?Z) )
Tk.callback_continue
end
Tk.callback_break
}, '%A')
e.bindtags(e.bindtags.unshift(asc_only))
ev_EXIT = TkVirtualEvent.new(['Control-x', 'Control-c'],
'Control-q', ['Escape', 'q'], 'Alt-q')
# generate Bitmap
exit_bmp = TkBitmapImage.new('data'=><<'END')
#define exit-door_width 35
#define exit-door_height 22
static unsigned char exit-door_bits = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfc, 0xff, 0x01, 0x00, 0x00,
0x7c, 0x00, 0x01, 0x00, 0x00, 0x7c, 0x00, 0x01, 0x00, 0x00, 0x7c, 0x00,
0x01, 0x00, 0x00, 0x7c, 0x00, 0x01, 0x00, 0x08, 0x7c, 0x00, 0x01, 0x00,
0x18, 0x7c, 0x00, 0x01, 0x00, 0x38, 0x7c, 0x00, 0x01, 0xe9, 0x7f, 0x7c,
0x00, 0x01, 0xd2, 0xff, 0x7c, 0x03, 0x01, 0xa4, 0xff, 0x7d, 0x03, 0x01,
0xd2, 0xff, 0x7c, 0x00, 0x01, 0xe9, 0x7f, 0x7c, 0x00, 0x01, 0x00, 0x38,
0x7c, 0x00, 0x01, 0x00, 0x18, 0x7c, 0x00, 0x01, 0x00, 0x08, 0x7c, 0x00,
0x01, 0x00, 0x00, 0x7c, 0x00, 0x01, 0x00, 0x00, 0x7c, 0x00, 0x01, 0x00,
0x00, 0x7c, 0x00, 0x01, 0x00, 0x00, 0xfc, 0xff, 0x01, 0x00, 0x00, 0x00,
0x00, 0x00};
END
TkButton.new(nil, 'image'=>exit_bmp, 'anchor'=>'e',
'command'=>proc{TkRoot.new.destroy}) {|b|
root.bind(ev_EXIT, proc{b.flash; b.invoke})
# create a label widget on a button widget
txt = TkLabel.new(b, 'text'=>'EXIT',
'padx'=>2).place('anchor'=>'w', 'x'=>2, 'rely'=>0.5)
# set button width to 'padding of place' + 'label width' + 'image width'
width (2 + TkWinfo.reqwidth(txt) + exit_bmp.width)
# transfer events on the label to the button
bindtags.each{|tags|
tags.bindinfo.each{|ev|
txt.bind(ev, proc{Tk.event_generate(b, ev)})
}
}
# control 'Enter' and 'Leave' event
# NOT send 'Enter' and 'Leave' on the label to the button
txt.bind_remove('Enter')
txt.bind_remove('Leave')
# change the labels background when 'Enter' or 'Leave' on the button
b.bind_append('Enter', proc{txt.background(b.activebackground)})
b.bind_append('Leave', proc{txt.background(b.background)})
}.pack('side'=>'bottom', 'anchor'=>'e')
font = TkFont.new(['courier', 16, ['italic']])
TkFrame.new{|f2|
TkLabel.new(f2, 'text'=>'Hello, ', 'font'=>font).pack('side'=>'left')
TkLabel.new(f2, 'text'=>v.value, 'font'=>font){|l|
e.bind('Return', proc{l.text v.value})
}.pack('side'=>'left')
TkLabel.new(f2, 'text'=>'!!', 'font'=>font).pack('side'=>'left')
}.pack('side'=>'bottom')
Tk.mainloop
print "Bye, #{v.value} ...\n"
--------------------------------------------------------
--
Hidetoshi NAGAI (nagai@ai.kyutech.ac.jp)