1# combo.rb --
2#
3# This demonstration script creates several combobox widgets.
4#
5# based on "Id: combo.tcl,v 1.3 2007/12/13 15:27:07 dgp Exp"
6
7if defined?($combo_demo) && $combo_demo
8  $combo_demo.destroy
9  $combo_demo = nil
10end
11
12$combo_demo = TkToplevel.new {|w|
13  title("Combobox Demonstration")
14  iconname("combo")
15  positionWindow(w)
16}
17
18base_frame = TkFrame.new($combo_demo).pack(:fill=>:both, :expand=>true)
19
20Ttk::Label.new(base_frame, :font=>$font, :wraplength=>'5i', :justify=>:left,
21               :text=><<EOL).pack(:side=>:top, :fill=>:x)
22Three different combo-boxes are displayed below. \
23You can add characters to the first \
24one by pointing, clicking and typing, just as with an entry; pressing \
25Return will cause the current value to be added to the list that is \
26selectable from the drop-down list, and you can choose other values \
27by pressing the Down key, using the arrow keys to pick another one, \
28and pressing Return again. The second combo-box is fixed to a \
29particular value, and cannot be modified at all. The third one only \
30allows you to select values from its drop-down list of Australian \
31cities.
32EOL
33
34## variables
35firstValue  = TkVariable.new
36secondValue = TkVariable.new
37ozCity      = TkVariable.new
38
39## See Code / Dismiss buttons
40Ttk::Frame.new(base_frame) {|frame|
41  sep = Ttk::Separator.new(frame)
42  Tk.grid(sep, :columnspan=>4, :row=>0, :sticky=>'ew', :pady=>2)
43  TkGrid('x',
44         Ttk::Button.new(frame, :text=>'See Variables',
45                         :image=>$image['view'], :compound=>:left,
46                         :command=>proc{
47                           showVars(base_frame,
48                                    ['firstVariable', firstValue],
49                                    ['secondVariable', secondValue],
50                                    ['ozCity', ozCity])
51                         }),
52         Ttk::Button.new(frame, :text=>'See Code',
53                         :image=>$image['view'], :compound=>:left,
54                         :command=>proc{showCode 'combo'}),
55         Ttk::Button.new(frame, :text=>'Dismiss',
56                         :image=>$image['delete'], :compound=>:left,
57                         :command=>proc{
58                           $combo_demo.destroy
59                           $combo_demo = nil
60                         }),
61         :padx=>4, :pady=>4)
62  grid_columnconfigure(0, :weight=>1)
63  pack(:side=>:bottom, :fill=>:x)
64}
65
66frame = Ttk::Frame.new(base_frame).pack(:fill=>:both, :expand=>true)
67
68australianCities = [
69  'Canberra', 'Sydney', 'Melbourne', 'Perth', 'Adelaide', 'Brisbane',
70  'Hobart', 'Darwin', 'Alice Springs'
71]
72
73
74secondValue.value = 'unchangable'
75ozCity.value = 'Sydney'
76
77Tk.pack(Ttk::Labelframe.new(frame, :text=>'Fully Editable'){|f|
78          Ttk::Combobox.new(f, :textvariable=>firstValue){|b|
79            b.bind('Return', '%W'){|w|
80              w.values <<= w.value unless w.values.include?(w.value)
81            }
82          }.pack(:pady=>5, :padx=>10)
83        },
84
85        Ttk::LabelFrame.new(frame, :text=>'Disabled'){|f|
86          Ttk::Combobox.new(f, :textvariable=>secondValue, :state=>:disabled) .
87            pack(:pady=>5, :padx=>10)
88        },
89
90        Ttk::LabelFrame.new(frame, :text=>'Defined List Only'){|f|
91          Ttk::Combobox.new(f, :textvariable=>ozCity, :state=>:readonly,
92                            :values=>australianCities) .
93            pack(:pady=>5, :padx=>10)
94        },
95
96        :side=>:top, :pady=>5, :padx=>10)
97