1#!/usr/bin/env ruby
2
3require 'multi-tk'
4
5TkMessage.new(:text => <<EOM).pack
6This is a sample of the safe-Tk slave interpreter. \
7On the slave interpreter, 'tkoptdb.rb' demo is running.
8( NOTE:: a safe-Tk interpreter can't read options \
9from a file. Options are given by the master interpreter \
10in this script. )
11The window shown this message is a root widget of \
12the default master interpreter. The other window \
13is a toplevel widget of the master interpreter, and it \
14has a container frame of the safe-Tk slave interpreter.
15'exit' on the slave interpreter exits the slave only. \
16You can also delete the slave by the button on the toplevel widget.
17EOM
18
19if ENV['LANG'] =~ /^ja/
20  # read Japanese resource
21  ent = TkOptionDB.read_entries(File.expand_path('resource.ja',
22                                                 File.dirname(__FILE__)),
23                                'utf-8')
24else
25  # read English resource
26  ent = TkOptionDB.read_entries(File.expand_path('resource.en',
27                                                File.dirname(__FILE__)))
28end
29
30file = File.expand_path('tkoptdb.rb', File.dirname(__FILE__))
31
32ip = MultiTkIp.new_safeTk{
33  # When a block is given to 'new_safeTk' method,
34  # the block is evaluated on $SAFE==4.
35  ent.each{|pat, val| Tk.tk_call('option', 'add', pat, val)}
36}
37
38print "ip.eval_proc{$SAFE} ==> ", ip.eval_proc{$SAFE}, "\n"
39
40print "\ncall 'ip.wait_on_mainloop = false'\n"
41print "If 'ip.wait_on_mainloop? == true', ",
42  "when 'mainloop' is called on 'ip.eval_proc', ",
43  "'ip.eval_proc' does't return while the root window exists.\n",
44  "If you want to avoid that, set wait_on_mainloop to false. ",
45  "Then the mainloop in the eval_proc returns soon ",
46  "and the following steps are evaluated. \n",
47  "If you hate the both of them, use 'ip.bg_eval_proc' or ",
48  "wrap 'ip.eval_proc' by a thread.\n"
49
50ip.wait_on_mainloop = false
51
52ret = ip.eval_proc{
53  # When a block is given to 'eval_proc' method,
54  # the block is evaluated on the IP's current safe level.
55  # So, the followings raises an exception.
56  # An Exception object of the exception is returned as a
57  # return value of this method.
58
59  load file
60}
61print "\nip.eval_proc{}, which includes insecure operiation in the given block, returns an exception object: ", ret.inspect, "\n"
62
63print "If a proc object is given, the proc is evaluated on the safe-level which is kept on the proc :: ip.eval_proc( proc{$SAFE} ) ==> ", ip.eval_proc(proc{$SAFE}), "\n"
64
65safe0_cmd = Proc.new{
66  print 'safe0_cmd safe-level == ', $SAFE, "\n"
67  # This proc object keeps current safe-level ($SAFE==0).
68  load file
69}
70ip.eval_proc{safe0_cmd.call}
71
72# Tk.mainloop is ignored on the slave-IP
73Tk.mainloop
74