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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
|
# -*- coding: utf-8 -*-
#
# menus widget demo (called by 'widget')
#
# toplevel widget ãåå¨ããã°åé¤ãã
if defined?($menu8x_demo) && $menu8x_demo
$menu8x_demo.destroy
$menu8x_demo = nil
end
# demo ç¨ã® toplevel widget ãçæ
$menu8x_demo = TkToplevel.new {|w|
title("Menu Demonstration (Tk8.x)")
iconname("menu")
positionWindow(w)
}
base_frame = TkFrame.new($menu8x_demo).pack(:fill=>:both, :expand=>true)
# version check
if $tk_version.to_f < 8.0
# label çæ
TkLabel.new(base_frame,'font'=>$font,'wraplength'=>'4i','justify'=>'left') {
text("å®è¡ãããã¨ããã¹ã¯ãªãã㯠Tk8.0 以ä¸ã§å©ç¨ã§ããæ©è½ãå©ç¨ãã¦ãããããããªãã® Ruby#{VERSION}/Tk#{$tk_version}#{(Tk::JAPANIZED_TK)? 'jp': ''} ã§ã¯æ£å¸¸ã«å®è¡ã§ãã¾ããããã£ã¦ãã¢ã®å®è¡ãä¸æ¢ãã¾ããããã ããä¸ã®ã³ã¼ãåç
§ãã¿ã³ãæ¼ããã¨ã§ãå®è¡ãä¸æ¢ãããã¹ã¯ãªããã®ã½ã¼ã¹ãåç
§ãããã¨ã¯å¯è½ã§ãã")
}.pack('side'=>'top')
# frame çæ
TkFrame.new(base_frame) {|frame|
TkButton.new(frame) {
#text 'äºè§£'
text 'éãã'
command proc{
tmppath = $menu8x_demo
$menu8x_demo = nil
tmppath.destroy
}
}.pack('side'=>'left', 'expand'=>'yes')
TkButton.new(frame) {
text 'ã³ã¼ãåç
§'
command proc{showCode 'menu8x'}
}.pack('side'=>'left', 'expand'=>'yes')
}.pack('side'=>'bottom', 'fill'=>'x', 'pady'=>'2m')
else ; # Tk8.x
begin
windowingsystem = Tk.windowingsystem()
rescue
windowingsystem = ""
end
# label çæ
TkLabel.new(base_frame,'font'=>$font,'wraplength'=>'4i','justify'=>'left') {
if $tk_platform['platform'] == 'macintosh' ||
windowingsystem == "classic" || windowingsystem == "aqua"
text("ãã®ã¦ã£ã³ãã¦ã¯æ§ã
ãªã¡ãã¥ã¼ã¨ã«ã¹ã±ã¼ãã¡ãã¥ã¼ããæ§æããã¦ãã¾ããCommand-X ãå
¥åããã¨ãXãã³ãã³ããã¼è¨å·ã«ç¶ãã¦è¡¨ç¤ºããã¦ããæåãªãã°ãã¢ã¯ã»ã©ã¬ã¼ã¿ã使ã£ãé
ç®èµ·åãè¡ããã¨ãã§ãã¾ããã¡ãã¥ã¼è¦ç´ ä¸ãæå¾ã®ãã®ã¯ããã®ã¡ãã¥ã¼ã®æåã®é
ç®ãé¸æãããã¨ã§ç¬ç«ããããã¨ãã§ãã¾ãã")
else
text("ãã®ã¦ã£ã³ãã¦ã¯æ§ã
ãªã¡ãã¥ã¼ã¨ã«ã¹ã±ã¼ãã¡ãã¥ã¼ããæ§æããã¦ãã¾ããAlt-X ãå
¥åããã¨ãXãã¡ãã¥ã¼ã«ã¢ã³ãã¼ã©ã¤ã³ä»ãã§è¡¨ç¤ºããã¦ããæåãªãã°ããã¼ãã¼ãããã®æå®ãã§ãã¾ããç¢å°ãã¼ã§ã¡ãã¥ã¼ã®ãã©ãã¼ã¹ãå¯è½ã§ããã¡ãã¥ã¼ãæå®ãããéã«ã¯ãã¹ãã¼ã¹ãã¼ã§å®è¡ãããã¨ãã§ãã¾ãããããã¯ãã¢ã³ãã¼ã©ã¤ã³ä»ãã®æåãå
¥åãããã¨ã§ãå®è¡ã§ãã¾ããã¡ãã¥ã¼ã®ã¨ã³ããªãã¢ã¯ã»ã©ã¬ã¼ã¿ãæã£ã¦ããå ´åã¯ããã®ã¢ã¯ã»ã©ã¬ã¼ã¿ãå
¥åãããã¨ã§ã¡ãã¥ã¼ãæå®ãããã¨ãªãã«å®è¡ãããã¨ãã§ãã¾ããã¡ãã¥ã¼è¦ç´ ä¸ãæå¾ã®ãã®ã¯ããã®ã¡ãã¥ã¼ã®æåã®é
ç®ãé¸æãããã¨ã§ç¬ç«ããããã¨ãã§ãã¾ãã")
end
}.pack('side'=>'top')
# ç¶æ
表示ã®çæ
$menu8xstatus = TkVariable.new(" ")
TkFrame.new(base_frame) {|frame|
TkLabel.new(frame, 'textvariable'=>$menu8xstatus, 'relief'=>'sunken',
'bd'=>1, 'font'=>['Helvetica', '10'], 'anchor'=>'w')\
.pack('side'=>'left', 'padx'=>2, 'expand'=>'yes', 'fill'=>'both')
}.pack('side'=>'bottom', 'fill'=>'x', 'pady'=>2)
# frame çæ
TkFrame.new(base_frame) {|frame|
TkButton.new(frame) {
#text 'äºè§£'
text 'éãã'
command proc{
tmppath = $menu8x_demo
$menu8x_demo = nil
tmppath.destroy
}
}.pack('side'=>'left', 'expand'=>'yes')
TkButton.new(frame) {
text 'ã³ã¼ãåç
§'
command proc{showCode 'menu8x'}
}.pack('side'=>'left', 'expand'=>'yes')
}.pack('side'=>'bottom', 'fill'=>'x', 'pady'=>'2m')
# menu çæ
TkMenu.new($menu8x_demo, 'tearoff'=>false) {|m|
TkMenu.new(m, 'tearoff'=>false) {|file_menu|
m.add('cascade', 'label'=>'File', 'menu'=>file_menu, 'underline'=>0)
add('command', 'label'=>'éã ...', 'command'=>proc{fail 'ããã¯ããã¢ã§ãã®ã§"éã ..."ã«å¯¾ããã¢ã¯ã·ã§ã³ã¯å®ç¾©ããã¦ãã¾ããã'})
add('command', 'label'=>'æ°è¦', 'command'=>proc{fail 'ããã¯ããã¢ã§ãã®ã§"æ°è¦"ã«å¯¾ããã¢ã¯ã·ã§ã³ã¯å®ç¾©ããã¦ãã¾ããã'})
add('command', 'label'=>'ä¿å', 'command'=>proc{fail 'ããã¯ããã¢ã§ãã®ã§"ä¿å"ã«å¯¾ããã¢ã¯ã·ã§ã³ã¯å®ç¾©ããã¦ãã¾ããã'})
add('command', 'label'=>'ä¿å(æå®) ...', 'command'=>proc{fail 'ããã¯ããã¢ã§ãã®ã§"ä¿å(æå®) ..."ã«å¯¾ããã¢ã¯ã·ã§ã³ã¯å®ç¾©ããã¦ãã¾ããã'})
add('separator')
add('command', 'label'=>'ããªã³ãè¨å® ...', 'command'=>proc{fail 'ããã¯ããã¢ã§ãã®ã§"ããªã³ãè¨å® ..."ã«å¯¾ããã¢ã¯ã·ã§ã³ã¯å®ç¾©ããã¦ãã¾ããã'})
add('command', 'label'=>'ããªã³ã ...', 'command'=>proc{fail 'ããã¯ããã¢ã§ãã®ã§"ããªã³ã ..."ã«å¯¾ããã¢ã¯ã·ã§ã³ã¯å®ç¾©ããã¦ãã¾ããã'})
add('separator')
add('command', 'label'=>'çµäº', 'command'=>proc{$menu8x_demo.destroy})
}
if $tk_platform['platform'] == 'macintosh' ||
windowingsystem == "classic" || windowingsystem == "aqua"
modifier = 'Command'
elsif $tk_platform['platform'] == 'windows'
modifier = 'Control'
else
modifier = 'Meta'
end
TkMenu.new(m, 'tearoff'=>false) {|basic_menu|
m.add('cascade', 'label'=>'Basic', 'menu'=>basic_menu, 'underline'=>0)
add('command', 'label'=>'ä½ãããªãé·ãã¨ã³ããª')
['A','B','C','D','E','F','G'].each{|c|
# add('command', 'label'=>"æå \"#{c}\" ãå°å", 'underline'=>4,
add('command', 'label'=>"Print letter \"#{c}\" (æå \"#{c}\" ãå°å)",
'underline'=>14, 'accelerator'=>"Meta+#{c}",
'command'=>proc{print c,"\n"}, 'accelerator'=>"#{modifier}+#{c}")
$menu8x_demo.bind("#{modifier}-#{c.downcase}", proc{print c,"\n"})
}
}
TkMenu.new(m, 'tearoff'=>false) {|cascade_menu|
m.add('cascade', 'label'=>'Cascades', 'menu'=>cascade_menu, 'underline'=>0)
add('command', 'label'=>'Print hello(ããã«ã¡ã¯)',
'command'=>proc{print "Hello(ããã«ã¡ã¯)\n"},
'accelerator'=>"#{modifier}+H", 'underline'=>6)
$menu8x_demo.bind("#{modifier}-h", proc{print "Hello(ããã«ã¡ã¯)\n"})
add('command', 'label'=>'Print goodbye(ããããªã)',
'command'=>proc{print "Goodbye(ããããªã)\n"},
'accelerator'=>"#{modifier}+G", 'underline'=>6)
$menu8x_demo.bind("#{modifier}-g", proc{print "Goodbye(ããããªã)\n"})
TkMenu.new(m, 'tearoff'=>false) {|cascade_check|
cascade_menu.add('cascade', 'label'=>'Check buttons(ãã§ãã¯ãã¿ã³)',
'menu'=>cascade_check, 'underline'=>0)
oil = TkVariable.new(0)
add('check', 'label'=>'ãªã¤ã«ç¹æ¤', 'variable'=>oil)
trans = TkVariable.new(0)
add('check', 'label'=>'ãã©ã³ã¹ããã·ã§ã³ç¹æ¤', 'variable'=>trans)
brakes = TkVariable.new(0)
add('check', 'label'=>'ãã¬ã¼ãç¹æ¤', 'variable'=>brakes)
lights = TkVariable.new(0)
add('check', 'label'=>'ã©ã¤ãç¹æ¤', 'variable'=>lights)
add('separator')
add('command', 'label'=>'ç¾å¨ã®å¤ã表示',
'command'=>proc{showVars($menu8x_demo,
['ãªã¤ã«ç¹æ¤', oil],
['ãã©ã³ã¹ããã·ã§ã³ç¹æ¤', trans],
['ãã¬ã¼ãç¹æ¤', brakes],
['ã©ã¤ãç¹æ¤', lights])} )
invoke 1
invoke 3
}
TkMenu.new(m, 'tearoff'=>false) {|cascade_radio|
cascade_menu.add('cascade', 'label'=>'Radio buttons(ã©ã¸ãªãã¿ã³)',
'menu'=>cascade_radio, 'underline'=>0)
pointSize = TkVariable.new
add('radio', 'label'=>'10 ãã¤ã³ã', 'variable'=>pointSize, 'value'=>10)
add('radio', 'label'=>'14 ãã¤ã³ã', 'variable'=>pointSize, 'value'=>14)
add('radio', 'label'=>'18 ãã¤ã³ã', 'variable'=>pointSize, 'value'=>18)
add('radio', 'label'=>'24 ãã¤ã³ã', 'variable'=>pointSize, 'value'=>24)
add('radio', 'label'=>'32 ãã¤ã³ã', 'variable'=>pointSize, 'value'=>32)
add('separator')
style = TkVariable.new
add('radio', 'label'=>'ãã¼ãã³', 'variable'=>style, 'value'=>'roman')
add('radio', 'label'=>'ãã¼ã«ã', 'variable'=>style, 'value'=>'bold')
add('radio', 'label'=>'ã¤ã¿ãªãã¯', 'variable'=>style, 'value'=>'italic')
add('separator')
add('command', 'label'=>'ç¾å¨ã®å¤ã表示',
'command'=>proc{showVars($menu8x_demo,
['ãã¤ã³ããµã¤ãº', pointSize],
['ã¹ã¿ã¤ã«', style])} )
invoke 1
invoke 7
}
}
TkMenu.new(m, 'tearoff'=>false) {|icon_menu|
m.add('cascade', 'label'=>'Icons', 'menu'=>icon_menu, 'underline'=>0)
add('command',
'bitmap'=>'@'+[$demo_dir,'..',
'images','pattern.xbm'].join(File::Separator),
'hidemargin'=>1,
'command'=>proc{TkDialog.new('title'=>'Bitmap Menu Entry',
'text'=>'ä»ããªããé¸æããã¡ãã¥ã¼ã®é
ç®ã¯ããã¹ãã§ã¯ãªãããããããã表示ãã¦ãã¾ããããã以å¤ã®ç¹ã§ã¯ä»ã®ã¡ãã¥ã¼é
ç®ã¨å¤ããã¾ããã',
'bitmap'=>'', 'default'=>0,
'buttons'=>'äºè§£')} )
['info', 'questhead', 'error'].each{|icon|
add('command', 'bitmap'=>icon, 'hidemargin'=>1,
'command'=>proc{print "You invoked the #{icon} bitmap\n"})
}
entryconfigure(2, 'columnbreak'=>1)
}
TkMenu.new(m, 'tearoff'=>false) {|more_menu|
m.add('cascade', 'label'=>'More', 'menu'=>more_menu, 'underline'=>0)
[ 'ã¨ã³ããª','å¥ã®ã¨ã³ããª','ä½ãããªã','ã»ã¨ãã©ä½ãããªã',
'人çãæ義ãããã®ã«' ].each{|i|
add('command', 'label'=>i,
'command'=>proc{print "You invoked \"#{i}\"\n"})
}
}
TkMenu.new(m) {|colors_menu|
m.add('cascade', 'label'=>'Colors', 'menu'=>colors_menu, 'underline'=>1)
['red', 'orange', 'yellow', 'green', 'blue'].each{|c|
add('command', 'label'=>c, 'background'=>c,
'command'=>proc{print "You invoked \"#{c}\"\n"})
}
}
$menu8x_demo.configure('menu'=>m)
}
TkMenu.bind('<MenuSelect>',
proc{|w|
begin
label = w.entrycget('active', 'label')
rescue
label = ' '
end
$menu8xstatus.value = label
Tk.update('idletasks')
}, '%W')
end ; # Tk 8.x
|