summaryrefslogtreecommitdiff
path: root/jni/ruby/ext/tk/sample/tkoptdb.rb
blob: 0171e465b346513a964c2f709c41e51dcf4755aa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/usr/bin/env ruby
#
#  sample script of TkOptionDB
#
#  If 'LANG' environment variable's value is started by 'ja',
#  then read Japanese resource data and display Japanese button text.
#  In other case, read English resource data and display English text.
#
require "tk"

if __FILE__ == $0 || !TkCore::INTERP.safe?
  if ENV['LANG'] =~ /^ja/
    # read Japanese resource
    TkOptionDB.read_with_encoding(File.expand_path('resource.ja',
                                                   File.dirname(__FILE__)),
                                  'utf-8')
  else
    # read English resource
    TkOptionDB.readfile(File.expand_path('resource.en',
                                         File.dirname(__FILE__)))
  end
end

# 'show_msg' and 'bye_msg' procedures can be defined on BTN_CMD resource.
# Those procedures are called under $SAFE==2
cmd = TkOptionDB.new_proc_class(:BTN_CMD, [:show_msg, :bye_msg], 3) {
  # If you want to check resource string (str),
  # please define __check_proc_string__(str) like this.
  class << self
    def __check_proc_string__(str)
      print "($SAFE=#{$SAFE} >>>) check!! str.tainted?::#{str.tainted?}"
      str.untaint
      print "==>#{str.tainted?} (<<< $SAFE=#{$SAFE}): "
      str
    end
    private :__check_proc_string__
  end
}

# following two frame widgets use same database entry
TkFrame.new(:class=>'BtnFrame'){|f|
  pack(:padx=>5, :pady=>5)
  TkButton.new(:parent=>f, :widgetname=>'hello'){
    command proc{
      print "($SAFE=#{$SAFE} >>>) : "
      cmd.show_msg(TkOptionDB.inspect)
      print "(<<< $SAFE=#{$SAFE})"
    }
    pack(:fill=>:x, :padx=>10, :pady=>10)
  }
  TkButton.new(:command=>proc{
                 print "($SAFE=#{$SAFE} >>>) : "
                 cmd.bye_msg
                 print "(<<< $SAFE=#{$SAFE} ) : "
                 exit
               },
               :parent=>f, :widgetname=>'quit'){
    pack(:fill=>:x, :padx=>10, :pady=>10)
  }
}

class BtnFrame < TkFrame; end
BtnFrame.new{|f|
  pack(:padx=>5, :pady=>5)
  TkButton.new(:parent=>f, :widgetname=>'hello'){
    command proc{
      print "($SAFE=#{$SAFE} >>>) : "
      cmd.show_msg(TkOptionDB.inspect)
      print "(<<< $SAFE=#{$SAFE})"
    }
    pack(:fill=>:x, :padx=>10, :pady=>10)
  }
  TkButton.new(:command=>proc{
                 print "($SAFE=#{$SAFE} >>>) : "
                 cmd.bye_msg
                 print "(<<< $SAFE=#{$SAFE})"
                 exit
               },
               :parent=>f, :widgetname=>'quit'){
    pack(:fill=>:x, :padx=>10, :pady=>10)
  }
}

# if unknown class, use default option values
TkFrame.new(:class=>'BtnFrame2'){|f|
  pack(:padx=>5, :pady=>5)
  TkButton.new(:parent=>f, :widgetname=>'hello'){
    command proc{
      print "($SAFE=#{$SAFE} >>>) : "
      cmd.show_msg(TkOptionDB.inspect)
      print "(<<< $SAFE=#{$SAFE})"
    }
    pack(:fill=>:x, :padx=>10, :pady=>10)
  }
  TkButton.new(:command=>proc{
                 print "($SAFE=#{$SAFE} >>>) : "
                 cmd.bye_msg
                 print "(<<< $SAFE=#{$SAFE})"
                 exit
               },
               :parent=>f, :widgetname=>'quit'){
    pack(:fill=>:x, :padx=>10, :pady=>10)
  }
}

Tk.mainloop