~ubuntu-branches/ubuntu/hardy/ruby1.8/hardy-updates

« back to all changes in this revision

Viewing changes to ext/tk/sample/safe-tk.rb

  • Committer: Bazaar Package Importer
  • Author(s): akira yamada
  • Date: 2007-03-13 22:11:58 UTC
  • mfrom: (1.1.5 upstream)
  • Revision ID: james.westby@ubuntu.com-20070313221158-h3oql37brlaf2go2
Tags: 1.8.6-1
* new upstream version, 1.8.6.
* libruby1.8 conflicts with libopenssl-ruby1.8 (< 1.8.6) (closes: #410018)
* changed packaging style to cdbs from dbs.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env ruby
 
2
# This script is a sample of MultiTkIp class
 
3
 
 
4
require "multi-tk"
 
5
 
 
6
###############################
 
7
 
 
8
TkLabel.new(:text=>'This is the Default Master Ipnterpreter').pack(:padx=>5, :pady=>3)
 
9
TkButton.new(:text=>'QUIT', :command=>proc{exit}).pack(:pady=>3)
 
10
TkFrame.new(:borderwidth=>2, :height=>3, 
 
11
            :relief=>:sunken).pack(:fill=>:x, :expand=>true,
 
12
                                   :padx=>10, :pady=>7)
 
13
 
 
14
###############################
 
15
 
 
16
puts "---- create a safe slave IP with Ruby's safe-level == 1 ----------"
 
17
ip = MultiTkIp.new_safe_slave(1)
 
18
 
 
19
puts "\n---- create procs ----------"
 
20
puts 'x = proc{p [\'proc x\', "$SAFE==#{$SAFE}"]; exit}'
 
21
x = proc{p ['proc x', "$SAFE==#{$SAFE}"]; exit}
 
22
TkLabel.new(:text=>'x = proc{p [\'proc x\', "$SAFE==#{$SAFE}"]; exit}', 
 
23
            :anchor=>:w).pack(:fill=>:x)
 
24
 
 
25
puts 'y = proc{|label| p [\'proc y\', "$SAFE==#{$SAFE}", label]; label.text($SAFE)}'
 
26
y = proc{|label| p ['proc y', "$SAFE==#{$SAFE}", label]; label.text($SAFE)}
 
27
TkLabel.new(:text=>'y = proc{|label| p [\'proc y\', "$SAFE==#{$SAFE}", label]; label.text($SAFE)}', 
 
28
            :anchor=>:w).pack(:fill=>:x)
 
29
 
 
30
puts 'z = proc{p [\'proc z\', "$SAFE==#{$SAFE}"]; exit}'
 
31
z = proc{p ['proc z', "$SAFE==#{$SAFE}"]; exit}
 
32
TkLabel.new(:text=>'z = proc{p [\'proc z\', "$SAFE==#{$SAFE}"]; exit}', 
 
33
            :anchor=>:w).pack(:fill=>:x)
 
34
 
 
35
puts "\n---- call 1st eval_proc ----------"
 
36
print 'lbl = '
 
37
p lbl = ip.eval_proc{
 
38
  TkLabel.new(:text=>"1st eval_proc : $SAFE == #{$SAFE}").pack
 
39
 
 
40
  f = TkFrame.new.pack
 
41
  TkLabel.new(f, :text=>"$SAFE == ").pack(:side=>:left)
 
42
  # TkLabel.new(f, :text=>" (<-- 'lbl' widget is here)").pack(:side=>:right)
 
43
  l = TkLabel.new(f).pack(:side=>:right)
 
44
 
 
45
  TkButton.new(:text=>':command=>proc{l.text($SAFE)}', 
 
46
               :command=>proc{l.text($SAFE)}).pack(:fill=>:x, :padx=>5)
 
47
  TkButton.new(:text=>':command=>x', :command=>x).pack(:fill=>:x, :padx=>5)
 
48
  TkButton.new(:text=>':command=>proc{exit}', 
 
49
               :command=>proc{exit}).pack(:fill=>:x, :padx=>5)
 
50
  TkFrame.new(:borderwidth=>2, :height=>3, 
 
51
              :relief=>:sunken).pack(:fill=>:x, :expand=>true,
 
52
                                     :padx=>10, :pady=>7)
 
53
  l # return the label widget
 
54
}
 
55
 
 
56
puts "\n---- change the safe slave IP's safe-level ==> 3 ----------"
 
57
ip.safe_level = 3
 
58
 
 
59
puts "\n---- call 2nd eval_proc ----------"
 
60
p ip.eval_proc(proc{
 
61
                 TkLabel.new(:text=>"2nd eval_proc : $SAFE == #{$SAFE}").pack
 
62
                 f = TkFrame.new.pack
 
63
                 TkLabel.new(f, :text=>"$SAFE == ").pack(:side=>:left)
 
64
                 l = TkLabel.new(f, :text=>$SAFE).pack(:side=>:right)
 
65
                 TkButton.new(:text=>':command=>proc{l.text($SAFE)}', 
 
66
                              :command=>proc{l.text($SAFE)}).pack(:fill=>:x, 
 
67
                                                                  :padx=>5)
 
68
                 TkButton.new(:text=>':command=>proc{y.call(l)}', 
 
69
                              :command=>proc{y.call(l)}).pack(:fill=>:x, 
 
70
                                                              :padx=>5)
 
71
                 TkButton.new(:text=>':command=>proc{Thread.new(l, &y).value}',
 
72
                              :command=>proc{
 
73
                                Thread.new(l, &y).value
 
74
                              }).pack(:fill=>:x, :padx=>5)
 
75
                 TkButton.new(:text=>':command=>proc{z.call}', 
 
76
                              :command=>proc{z.call}).pack(:fill=>:x, :padx=>5)
 
77
                 TkFrame.new(:borderwidth=>2, :height=>3, 
 
78
                             :relief=>:sunken).pack(:fill=>:x, :expand=>true,
 
79
                                                    :padx=>10, :pady=>7)
 
80
               })
 
81
 
 
82
puts "\n---- call 1st and 2nd eval_str ----------"
 
83
p bind = ip.eval_str('
 
84
  TkLabel.new(:text=>"1st and 2nd eval_str : $SAFE == #{$SAFE}").pack
 
85
  f = TkFrame.new.pack
 
86
  TkLabel.new(f, :text=>"$SAFE == ").pack(:side=>:left)
 
87
  l = TkLabel.new(f, :text=>$SAFE).pack(:side=>:right)
 
88
  TkButton.new(:text=>":command=>proc{y.call(l)}", 
 
89
               :command=>proc{y.call(l)}).pack(:fill=>:x, :padx=>5)
 
90
  binding
 
91
', binding)
 
92
 
 
93
p ip.eval_str("
 
94
  TkButton.new(:text=>':command=>proc{ l.text = $SAFE }', 
 
95
               :command=>proc{ l.text = $SAFE }).pack(:fill=>:x, :padx=>5)
 
96
  TkFrame.new(:borderwidth=>2, :height=>3, 
 
97
              :relief=>:sunken).pack(:fill=>:x, :expand=>true,
 
98
                                     :padx=>10, :pady=>7)
 
99
", bind)
 
100
 
 
101
puts "\n---- change the safe slave IP's safe-level ==> 4 ----------"
 
102
ip.safe_level = 4
 
103
 
 
104
puts "\n---- call 3rd and 4th eval_proc ----------"
 
105
p ip.eval_proc{
 
106
  TkLabel.new(:text=>"3rd and 4th eval_proc : $SAFE == #{$SAFE}").pack
 
107
}
 
108
p ip.eval_proc{
 
109
  TkButton.new(:text=>':command=>proc{ lbl.text = $SAFE }', 
 
110
               :command=>proc{ lbl.text = $SAFE }).pack(:fill=>:x, :padx=>5)
 
111
}
 
112
 
 
113
puts "\n---- start event-loop ( current $SAFE == #{$SAFE} ) ----------"
 
114
 
 
115
Tk.mainloop