1# states.rb
2#
3# This demonstration script creates a listbox widget that displays
4# the names of the 50 states in the United States of America.
5#
6# listbox widget demo 'states' (called by 'widget')
7#
8
9# toplevel widget
10if defined?($states_demo) && $states_demo
11  $states_demo.destroy
12  $states_demo = nil
13end
14
15# demo toplevel widget
16$states_demo = TkToplevel.new {|w|
17  title("Listbox Demonstration (states)")
18  iconname("states")
19  positionWindow(w)
20}
21
22base_frame = TkFrame.new($states_demo).pack(:fill=>:both, :expand=>true)
23
24# label
25msg = TkLabel.new(base_frame) {
26  font $font
27  wraplength '4i'
28  justify 'left'
29  text "A listbox containing the 50 states is displayed below, along with a scrollbar.  You can scan the list either using the scrollbar or by scanning.  To scan, press button 2 in the widget and drag up or down."
30}
31msg.pack('side'=>'top')
32
33# frame
34TkFrame.new(base_frame) {|frame|
35  TkButton.new(frame) {
36    text 'Dismiss'
37    command proc{
38      tmppath = $states_demo
39      $states_demo = nil
40      tmppath.destroy
41    }
42  }.pack('side'=>'left', 'expand'=>'yes')
43
44  TkButton.new(frame) {
45    text 'Show Code'
46    command proc{showCode 'states'}
47  }.pack('side'=>'left', 'expand'=>'yes')
48
49}.pack('side'=>'bottom', 'fill'=>'x', 'pady'=>'2m')
50
51# frame
52states_lbox = nil
53TkFrame.new(base_frame, 'borderwidth'=>'.5c') {|w|
54  s = TkScrollbar.new(w)
55  states_lbox = TkListbox.new(w) {
56    setgrid 1
57    height 12
58    yscrollcommand proc{|first,last| s.set first,last}
59  }
60  s.command(proc{|*args| states_lbox.yview(*args)})
61  s.pack('side'=>'right', 'fill'=>'y')
62  states_lbox.pack('side'=>'left', 'expand'=>1, 'fill'=>'both')
63}.pack('side'=>'top', 'expand'=>'yes', 'fill'=>'y')
64
65ins_data = [
66  'Alabama', 'Alaska', 'Arizona', 'Arkansas', 'California',
67  'Colorado', 'Connecticut', 'Delaware', 'Florida', 'Georgia',
68  'Hawaii', 'Idaho', 'Illinois',
69  'Indiana', 'Iowa', 'Kansas', 'Kentucky', 'Louisiana', 'Maine', 'Maryland',
70  'Massachusetts', 'Michigan', 'Minnesota', 'Mississippi', 'Missouri',
71  'Montana', 'Nebraska', 'Nevada', 'New_Hampshire', 'New_Jersey', 'New_Mexico',
72  'New_York', 'North_Carolina', 'North_Dakota',
73  'Ohio', 'Oklahoma', 'Oregon', 'Pennsylvania', 'Rhode_Island',
74  'South_Carolina', 'South_Dakota',
75  'Tennessee', 'Texas', 'Utah', 'Vermont', 'Virginia', 'Washington',
76  'West_Virginia', 'Wisconsin', 'Wyoming'
77]
78
79states_lbox.insert(0, *ins_data)
80
81