1#!/usr/bin/env ruby 2# This script is a sample of MultiTkIp class 3 4require "multi-tk" 5 6############################### 7 8TkLabel.new(:text=>'This is the Default Master Ipnterpreter').pack(:padx=>5, :pady=>3) 9TkButton.new(:text=>'QUIT', :command=>proc{exit}).pack(:pady=>3) 10TkFrame.new(:borderwidth=>2, :height=>3, 11 :relief=>:sunken).pack(:fill=>:x, :expand=>true, 12 :padx=>10, :pady=>7) 13 14safe0_p = proc{|*args| p args} 15 16############################### 17 18puts "---- create a safe slave IP with Ruby's safe-level == 1 ----------" 19ip = MultiTkIp.new_safe_slave(1){|*args| safe0_p["safe_slave safe_level == #{$SAFE}", args]} 20 21puts "\n---- create procs ----------" 22puts 'x = proc{p [\'proc x\', "$SAFE==#{$SAFE}"]; exit}' 23#x = proc{p ['proc x', "$SAFE==#{$SAFE}"]; exit} 24x = proc{p ['proc x', "$SAFE==#{$SAFE}"]; exit} 25TkLabel.new(:text=>'x = proc{p [\'proc x\', "$SAFE==#{$SAFE}"]; exit}', 26 :anchor=>:w).pack(:fill=>:x) 27 28puts 'y = proc{|label| p [\'proc y\', "$SAFE==#{$SAFE}", label]; label.text($SAFE)}' 29y = proc{|label| p ['proc y', "$SAFE==#{$SAFE}", label]; label.text($SAFE)} 30TkLabel.new(:text=>'y = proc{|label| p [\'proc y\', "$SAFE==#{$SAFE}", label]; label.text($SAFE)}', 31 :anchor=>:w).pack(:fill=>:x) 32 33puts 'z = proc{p [\'proc z\', "$SAFE==#{$SAFE}"]; exit}' 34z = proc{p ['proc z', "$SAFE==#{$SAFE}"]; exit} 35TkLabel.new(:text=>'z = proc{p [\'proc z\', "$SAFE==#{$SAFE}"]; exit}', 36 :anchor=>:w).pack(:fill=>:x) 37 38puts "\n---- call 1st eval_proc ----------" 39print 'lbl = ' 40p lbl = ip.eval_proc{ 41 TkLabel.new(:text=>"1st eval_proc : $SAFE == #{$SAFE}").pack 42 43 f = TkFrame.new.pack 44 TkLabel.new(f, :text=>"$SAFE == ").pack(:side=>:left) 45 # TkLabel.new(f, :text=>" (<-- 'lbl' widget is here)").pack(:side=>:right) 46 l = TkLabel.new(f).pack(:side=>:right) 47 48 TkButton.new(:text=>':command=>proc{l.text($SAFE)}', 49 :command=>proc{l.text($SAFE)}).pack(:fill=>:x, :padx=>5) 50 TkButton.new(:text=>':command=>x', :command=>x).pack(:fill=>:x, :padx=>5) 51 TkButton.new(:text=>':command=>proc{exit}', 52 :command=>proc{ 53 safe0_p["'exit' is called at $SAFE=#{$SAFE}"];exit} 54 ).pack(:fill=>:x, :padx=>5) 55 TkFrame.new(:borderwidth=>2, :height=>3, 56 :relief=>:sunken).pack(:fill=>:x, :expand=>true, 57 :padx=>10, :pady=>7) 58 l # return the label widget 59} 60 61puts "\n---- change the safe slave IP's safe-level ==> 3 ----------" 62ip.safe_level = 3 63 64puts "\n---- call 2nd eval_proc ----------" 65p ip.eval_proc(proc{ 66 TkLabel.new(:text=>"2nd eval_proc : $SAFE == #{$SAFE}").pack 67 f = TkFrame.new.pack 68 TkLabel.new(f, :text=>"$SAFE == ").pack(:side=>:left) 69 l = TkLabel.new(f, :text=>$SAFE).pack(:side=>:right) 70 TkButton.new(:text=>':command=>proc{l.text($SAFE)}', 71 :command=>proc{l.text($SAFE)}).pack(:fill=>:x, 72 :padx=>5) 73 TkButton.new(:text=>':command=>proc{y.call(l)}', 74 :command=>proc{y.call(l)}).pack(:fill=>:x, 75 :padx=>5) 76 TkButton.new(:text=>':command=>proc{Proc.new(&y).call(l)}', 77 :command=>proc{ 78 Proc.new(&y).call(l) 79 }).pack(:fill=>:x, :padx=>5) 80 TkButton.new(:text=>':command=>proc{MultiTkIp._proc_on_current_safelevel(y).call(l)}', 81 :command=>proc{ 82 MultiTkIp._proc_on_current_safelevel(y).call(l) 83 }).pack(:fill=>:x, :padx=>5) 84if false && Object.const_defined?(:RubyVM) && ::RubyVM.class == Class 85 TkButton.new(:text=>':command=>proc{Thread.new(l, &y).value}', 86 :command=>proc{ 87 Thread.new(l, &y).value 88 }).pack(:fill=>:x, :padx=>5) 89else 90 # KNOWN BUG:: 91 # Current multi-tk.rb cannot support long term threads on callbacks. 92 # Such a thread freezes the Ruby/Tk process. 93end 94 TkButton.new(:text=>':command=>proc{z.call}', 95 :command=>proc{z.call}).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 }) 100 101puts "\n---- call 1st and 2nd eval_str ----------" 102p bind = ip.eval_str(' 103 TkLabel.new(:text=>"1st and 2nd eval_str : $SAFE == #{$SAFE}").pack 104 f = TkFrame.new.pack 105 TkLabel.new(f, :text=>"$SAFE == ").pack(:side=>:left) 106 l = TkLabel.new(f, :text=>$SAFE).pack(:side=>:right) 107 TkButton.new(:text=>":command=>proc{y.call(l)}", 108 :command=>proc{y.call(l)}).pack(:fill=>:x, :padx=>5) 109 binding 110', binding) 111 112p ip.eval_str(" 113 TkButton.new(:text=>':command=>proc{ l.text = $SAFE }', 114 :command=>proc{ l.text = $SAFE }).pack(:fill=>:x, :padx=>5) 115 TkFrame.new(:borderwidth=>2, :height=>3, 116 :relief=>:sunken).pack(:fill=>:x, :expand=>true, 117 :padx=>10, :pady=>7) 118", bind) 119 120puts "\n---- change the safe slave IP's safe-level ==> 4 ----------" 121ip.safe_level = 4 122 123puts "\n---- call 3rd and 4th eval_proc ----------" 124p ip.eval_proc{ 125 TkLabel.new(:text=>"3rd and 4th eval_proc : $SAFE == #{$SAFE}").pack 126} 127p ip.eval_proc{ 128 TkButton.new(:text=>':command=>proc{ lbl.text = $SAFE }', 129 :command=>proc{ lbl.text = $SAFE }).pack(:fill=>:x, :padx=>5) 130} 131 132puts "\n---- start event-loop ( current $SAFE == #{$SAFE} ) ----------" 133 134Tk.mainloop 135