1#!/usr/bin/env ruby
2
3require 'remote-tk'
4
5# start sub-process
6ip_name = 'remote_ip'
7ip_list = TkWinfo.interps
8fork{
9  exec "/usr/bin/env ruby -r tk -e \"Tk.appname('#{ip_name}');Tk.mainloop\""
10}
11sleep 1 until (app = (TkWinfo.interps - ip_list)[0]) && app =~ /^#{ip_name}/
12p TkWinfo.interps
13
14# create RemoteTkIp object
15ip = RemoteTkIp.new(app)
16
17# setup remote-ip window
18btns = []
19ip.eval_proc{
20  btns <<
21    TkButton.new(:command=>proc{
22                   puts 'This procesure is on the controller-ip (Ruby-side)'
23                 },
24                 :text=>'print on controller-ip (Ruby-side)').pack(:fill=>:x)
25
26  btns <<
27    TkButton.new(:command=>
28                   'puts {This procesure is on the remote-ip (Tk-side)}',
29                 :text=>'print on remote-ip (Tk-side)').pack(:fill=>:x)
30
31  btns <<
32    TkButton.new(:command=>
33                   'ruby {
34                     puts "This procedure is on the remote-ip (Ruby-side)"
35                     p Array.new(3,"ruby")
36                    }',
37                 :text=>'ruby cmd on the remote-ip').pack(:fill=>:x)
38
39  TkButton.new(:command=>'exit', :text=>'QUIT').pack(:fill=>:x)
40}
41
42# setup controller-ip window
43btns.each_with_index{|btn, idx|
44  # The scope of the eval-block of 'eval_proc' method is different from
45  # the enternal. If you want to pass local values to the eval-block,
46  # use arguments of eval_proc method. They are passed to block-arguments.
47  TkButton.new(:command=>proc{ip.eval_proc(btn){|b| b.flash}},
48               :text=>"flash button-#{idx}",
49               :padx=>10).pack(:padx=>10, :pady=>2)
50}
51
52TkButton.new(:command=>proc{exit}, :text=>'QUIT',
53             :padx=>10, :pady=>7).pack(:padx=>10, :pady=>7)
54
55# start eventloop
56Tk.mainloop
57