1#
2# menus widget demo (called by 'widget')
3#
4
5# toplevel widget
6if defined?($menu84_demo) && $menu84_demo
7  $menu84_demo.destroy
8  $menu84_demo = nil
9end
10
11# demo toplevel widget
12$menu84_demo = TkToplevel.new {|w|
13  title("File Selection Dialogs")
14  iconname("menu84")
15  positionWindow(w)
16}
17
18base_frame = TkFrame.new($menu84_demo).pack(:fill=>:both, :expand=>true)
19
20begin
21  windowingsystem = Tk.windowingsystem()
22rescue
23  windowingsystem = ""
24end
25
26# label
27TkLabel.new(base_frame,'font'=>$font,'wraplength'=>'4i','justify'=>'left') {
28  if $tk_platform['platform'] == 'macintosh' ||
29      windowingsystem == "classic" || windowingsystem == "aqua"
30    text("This window contains a menubar with cascaded menus.  You can invoke entries with an accelerator by typing Command+x, where \"x\" is the character next to the command key symbol. The rightmost menu can be torn off into a palette by dragging outside of its bounds and releasing the mouse.")
31  else
32    text("This window contains a menubar with cascaded menus.  You can post a menu from the keyboard by typing Alt+x, where \"x\" is the character underlined on the menu.  You can then traverse among the menus using the arrow keys.  When a menu is posted, you can invoke the current entry by typing space, or you can invoke any entry by typing its underlined character.  If a menu entry has an accelerator, you can invoke the entry without posting the menu just by typing the accelerator. The rightmost menu can be torn off into a palette by selecting the first item in the menu.")
33  end
34}.pack('side'=>'top')
35
36
37menustatus = TkVariable.new("    ")
38TkFrame.new(base_frame) {|frame|
39  TkLabel.new(frame, 'textvariable'=>menustatus, 'relief'=>'sunken',
40              'bd'=>1, 'font'=>['Helvetica', '10'],
41              'anchor'=>'w').pack('side'=>'left', 'padx'=>2,
42                                  'expand'=>true, 'fill'=>'both')
43  pack('side'=>'bottom', 'fill'=>'x', 'pady'=>2)
44}
45
46
47# frame
48TkFrame.new(base_frame) {|frame|
49  TkButton.new(frame) {
50    text 'Dismiss'
51    command proc{
52      tmppath = $menu84_demo
53      $menu84_demo = nil
54      tmppath.destroy
55    }
56  }.pack('side'=>'left', 'expand'=>'yes')
57
58  TkButton.new(frame) {
59    text 'Show Code'
60    command proc{showCode 'menu84'}
61  }.pack('side'=>'left', 'expand'=>'yes')
62}.pack('side'=>'bottom', 'fill'=>'x', 'pady'=>'2m')
63
64
65# create menu frame
66$menu84_frame = TkMenu.new($menu84_demo, 'tearoff'=>false)
67
68# menu
69TkMenu.new($menu84_frame, 'tearoff'=>false) {|m|
70  $menu84_frame.add('cascade', 'label'=>'File', 'menu'=>m, 'underline'=>0)
71  add('command', 'label'=>'Open...', 'command'=>proc{fail 'this is just a demo: no action has been defined for the "Open..." entry'})
72  add('command', 'label'=>'New', 'command'=>proc{fail 'this is just a demo: no action has been defined for the "New" entry'})
73  add('command', 'label'=>'Save', 'command'=>proc{fail 'this is just a demo: no action has been defined for the "Save" entry'})
74  add('command', 'label'=>'Save As...', 'command'=>proc{fail 'this is just a demo: no action has been defined for the "Save As..." entry'})
75  add('separator')
76  add('command', 'label'=>'Print Setup...', 'command'=>proc{fail 'this is just a demo: no action has been defined for the "Print Setup..." entry'})
77  add('command', 'label'=>'Print...', 'command'=>proc{fail 'this is just a demo: no action has been defined for the "Print..." entry'})
78  add('separator')
79  add('command', 'label'=>'Dismiss Menus Demo', 'command'=>proc{$menu84_demo.destroy})
80}
81
82if $tk_platform['platform'] == 'macintosh' ||
83    windowingsystem = "classic" || windowingsystem = "aqua"
84  modifier = 'Command'
85elsif $tk_platform['platform'] == 'windows'
86  modifier = 'Control'
87else
88  modifier = 'Meta'
89end
90
91TkMenu.new($menu84_frame, 'tearoff'=>false) {|m|
92  $menu84_frame.add('cascade', 'label'=>'Basic', 'menu'=>m, 'underline'=>0)
93  add('command', 'label'=>'Long entry that does nothing')
94  ['A','B','C','D','E','F','G'].each{|c|
95    add('command', 'label'=>"Print letter \"#{c}\"",
96        'underline'=>14, 'accelerator'=>"Meta+#{c}",
97        'command'=>proc{print c,"\n"}, 'accelerator'=>"#{modifier}+#{c}")
98    $menu84_demo.bind("#{modifier}-#{c.downcase}", proc{print c,"\n"})
99  }
100}
101
102TkMenu.new($menu84_frame, 'tearoff'=>false) {|m|
103  $menu84_frame.add('cascade', 'label'=>'Cascades', 'menu'=>m, 'underline'=>0)
104  add('command', 'label'=>'Print hello',
105      'command'=>proc{print "Hello\n"},
106      'accelerator'=>"#{modifier}+H", 'underline'=>6)
107  $menu84_demo.bind("#{modifier}-h", proc{print "Hello\n"})
108  add('command', 'label'=>'Print goodbye',
109      'command'=>proc{print "Goodbye\n"},
110      'accelerator'=>"#{modifier}+G", 'underline'=>6)
111  $menu84_demo.bind("#{modifier}-g", proc{print "Goodbye\n"})
112
113  TkMenu.new(m, 'tearoff'=>false) {|cascade_check|
114    m.add('cascade', 'label'=>'Check buttons',
115          'menu'=>cascade_check, 'underline'=>0)
116    oil = TkVariable.new(0)
117    add('check', 'label'=>'Oil checked', 'variable'=>oil)
118    trans = TkVariable.new(0)
119    add('check', 'label'=>'Transmission checked', 'variable'=>trans)
120    brakes = TkVariable.new(0)
121    add('check', 'label'=>'Brakes checked', 'variable'=>brakes)
122    lights = TkVariable.new(0)
123    add('check', 'label'=>'Lights checked', 'variable'=>lights)
124    add('separator')
125    add('command', 'label'=>'Show current values',
126        'command'=>proc{showVars($menu84_demo,
127                                 ['oil', oil],
128                                 ['trans', trans],
129                                 ['brakes', brakes],
130                                 ['lights', lights])} )
131    invoke 1
132    invoke 3
133  }
134
135  TkMenu.new(m, 'tearoff'=>false) {|cascade_radio|
136    m.add('cascade', 'label'=>'Radio buttons',
137          'menu'=>cascade_radio, 'underline'=>0)
138    pointSize = TkVariable.new
139    add('radio', 'label'=>'10 point', 'variable'=>pointSize, 'value'=>10)
140    add('radio', 'label'=>'14 point', 'variable'=>pointSize, 'value'=>14)
141    add('radio', 'label'=>'18 point', 'variable'=>pointSize, 'value'=>18)
142    add('radio', 'label'=>'24 point', 'variable'=>pointSize, 'value'=>24)
143    add('radio', 'label'=>'32 point', 'variable'=>pointSize, 'value'=>32)
144    add('separator')
145    style = TkVariable.new
146    add('radio', 'label'=>'Roman', 'variable'=>style, 'value'=>'roman')
147    add('radio', 'label'=>'Bold', 'variable'=>style, 'value'=>'bold')
148    add('radio', 'label'=>'Italic', 'variable'=>style, 'value'=>'italic')
149    add('separator')
150    add('command', 'label'=>'Show current values',
151        'command'=>proc{showVars($menu84_demo,
152                                 ['pointSize', pointSize],
153                                 ['style', style])} )
154    invoke 1
155    invoke 7
156  }
157}
158
159TkMenu.new($menu84_frame, 'tearoff'=>false) {|m|
160  $menu84_frame.add('cascade', 'label'=>'Icons', 'menu'=>m, 'underline'=>0)
161  add('command', 'hidemargin'=>1,
162      'bitmap'=>'@'+[$demo_dir,'..',
163                      'images','pattern.xbm'].join(File::Separator),
164      'command'=>proc{TkDialog.new('title'=>'Bitmap Menu Entry',
165                                   'text'=>'The menu entry you invoked displays a bitmap rather than a text string.  Other than this, it is just like any other menu entry.',
166                                   'bitmap'=>'', 'default'=>0,
167                                   'buttons'=>'Dismiss')} )
168  ['info', 'questhead', 'error'].each{|icon|
169    add('command', 'bitmap'=>icon, 'hidemargin'=>1,
170        'command'=>proc{print "You invoked the #{icon} bitmap\n"})
171  }
172
173  entryconfigure(2, :columnbreak=>true)
174}
175
176TkMenu.new($menu84_frame, 'tearoff'=>false) {|m|
177  $menu84_frame.add('cascade', 'label'=>'More', 'menu'=>m, 'underline'=>0)
178  [ 'An entry','Another entry','Does nothing','Does almost nothing',
179    'Make life meaningful' ].each{|i|
180    add('command', 'label'=>i,
181        'command'=>proc{print "You invoked \"#{i}\"\n"})
182  }
183
184  m.entryconfigure('Does almost nothing',
185                   'bitmap'=>'questhead', 'compound'=>'left',
186                   'command'=>proc{
187                     TkDialog.new('title'=>'Compound Menu Entry',
188                                  'message'=>'The menu entry you invoked'+
189                                             'displays both a bitmap and '+
190                                             'a text string.  Other than '+
191                                             'this, it isjust like any '+
192                                             'other menu entry.',
193                                  'buttons'=>['OK'], 'bitmap'=>'')
194                   })
195}
196
197TkMenu.new($menu84_frame) {|m|
198  $menu84_frame.add('cascade', 'label'=>'Colors', 'menu'=>m, 'underline'=>0)
199  ['red', 'orange', 'yellow', 'green', 'blue'].each{|c|
200    add('command', 'label'=>c, 'background'=>c,
201        'command'=>proc{print "You invoked \"#{c}\"\n"})
202  }
203}
204
205$menu84_demo.menu($menu84_frame)
206
207TkMenu.bind('<MenuSelect>', proc{|w|
208              begin
209                label = w.entrycget('active', 'label')
210              rescue
211                label = "    "
212              end
213              menustatus.value = label
214              Tk.update(true)
215            }, '%W')
216