1# ttkbut.rb
2#
3# This demonstration script creates a toplevel window containing several
4# simple Ttk widgets, such as labels, labelframes, buttons, checkbuttons and
5# radiobuttons.
6#
7# based on "Id: ttkbut.tcl,v 1.4 2007/12/13 15:27:07 dgp Exp"
8
9if defined?($ttkbut_demo) && $ttkbut_demo
10  $ttkbut_demo.destroy
11  $ttkbut_demo = nil
12end
13
14$ttkbut_demo = TkToplevel.new {|w|
15  title("Simple Ttk Widgets")
16  iconname("ttkbut")
17  positionWindow(w)
18}
19
20base_frame = TkFrame.new($ttkbut_demo).pack(:fill=>:both, :expand=>true)
21
22Ttk::Label.new(base_frame, :font=>$font, :wraplength=>'4i', :justify=>:left,
23               :text=><<EOL).pack(:side=>:top, :fill=>:x)
24Ttk is the new Tk themed widget set. This is a Ttk themed label, \
25and below are three groups of Ttk widgets in Ttk labelframes. \
26The first group are all buttons that set the current application theme \
27when pressed. The second group contains three sets of checkbuttons, \
28with a separator widget between the sets. Note that the "Enabled" \
29button controls whether all the other themed widgets in this toplevel are \
30in the disabled state. The third group has a collection of linked \
31radiobuttons.
32EOL
33
34## Add buttons for setting the theme
35buttons = Ttk::Labelframe.new(base_frame, :text=>'Buttons')
36# Ttk::Style.theme_names.each{|theme|
37#   Ttk::Button.new(buttons, :text=>theme,
38#                   :command=>proc{Ttk::Style.theme_use theme}).pack(:pady=>2)
39# }
40Ttk.themes.each{|theme|
41  Ttk::Button.new(buttons, :text=>theme,
42                  :command=>proc{Ttk.set_theme theme}).pack(:pady=>2)
43}
44
45## Helper procedure for the top checkbutton
46def setState(root, value, *excepts)
47  return if excepts.member?(root)
48
49  ## Non-Ttk widgets (e.g. the toplevel) will fail, so make it silent
50  begin
51    root.state = value
52  rescue
53  end
54
55  ## Recursively invoke on all children of this root that are in the same
56  ## toplevel widget
57  root.winfo_children.each{|w|
58    setState(w, value, *excepts) if w.winfo_toplevel == root.winfo_toplevel
59  }
60end
61
62## Set up the checkbutton group
63checks = Ttk::Labelframe.new(base_frame, :text=>'Checkbuttons')
64enabled = TkVariable.new(true)
65e = Ttk::Checkbutton.new(checks, :text=>'Enabled', :variable=>enabled,
66                         :command=>proc{
67                           setState($ttkbut_demo,
68                                    ((enabled.bool)? "!disabled" : "disabled"),
69                                    e)
70                         })
71
72## See ttk_widget(n) for other possible state flags
73sep1 = Ttk::Separator.new(checks)
74sep2 = Ttk::Separator.new(checks)
75
76cheese  = TkVariable.new
77tomato  = TkVariable.new
78basil   = TkVariable.new
79oregano = TkVariable.new
80
81c1 = Ttk::Checkbutton.new(checks, :text=>'Cheese',  :variable=>cheese)
82c2 = Ttk::Checkbutton.new(checks, :text=>'Tomato',  :variable=>tomato)
83c3 = Ttk::Checkbutton.new(checks, :text=>'Basil',   :variable=>basil)
84c4 = Ttk::Checkbutton.new(checks, :text=>'Oregano', :variable=>oregano)
85
86Tk.pack(e, sep1, c1, c2, sep2, c3, c4, :fill=>:x, :pady=>2)
87
88## Set up the radiobutton group
89radios = Ttk::Labelframe.new(base_frame, :text=>'Radiobuttons')
90
91happyness = TkVariable.new
92
93r1 = Ttk::Radiobutton.new(radios, :variable=>happyness,
94                          :text=>'Great', :value=>'great')
95r2 = Ttk::Radiobutton.new(radios, :variable=>happyness,
96                          :text=>'Good', :value=>'good')
97r3 = Ttk::Radiobutton.new(radios, :variable=>happyness,
98                          :text=>'Ok', :value=>'ok')
99r4 = Ttk::Radiobutton.new(radios, :variable=>happyness,
100                          :text=>'Poor', :value=>'poor')
101r5 = Ttk::Radiobutton.new(radios, :variable=>happyness,
102                          :text=>'Awful', :value=>'awful')
103
104Tk.pack(r1, r2, r3, r4, r5, :fill=>:x, :padx=>3, :pady=>2)
105
106## See Code / Dismiss
107Ttk::Frame.new(base_frame) {|frame|
108  sep = Ttk::Separator.new(frame)
109  Tk.grid(sep, :columnspan=>4, :row=>0, :sticky=>'ew', :pady=>2)
110  TkGrid('x',
111         Ttk::Button.new(frame, :text=>'See Variables',
112                         :image=>$image['view'], :compound=>:left,
113                         :command=>proc{
114                           showVars(base_frame, ['enabled', enabled],
115                                    ['cheese', cheese], ['tomato', tomato],
116                                    ['basil', basil], ['oregano', oregano],
117                                    ['happyness', happyness])
118                         }),
119         Ttk::Button.new(frame, :text=>'See Code',
120                         :image=>$image['view'], :compound=>:left,
121                         :command=>proc{showCode 'ttkbut'}),
122         Ttk::Button.new(frame, :text=>'Dismiss',
123                         :image=>$image['delete'], :compound=>:left,
124                         :command=>proc{
125                           tmppath = $ttkbut_demo
126                           $ttkbut_demo = nil
127                           $showVarsWin[tmppath.path] = nil
128                           tmppath.destroy
129                         }),
130         :padx=>4, :pady=>4)
131  grid_columnconfigure(0, :weight=>1)
132  pack(:side=>:bottom, :fill=>:x, :expand=>true)
133}
134
135## Arrange things neatly
136f = Ttk::Frame.new(base_frame).pack(:fill=>:both, :expand=>true)
137f.lower
138Tk.grid(buttons, checks, radios, :in=>f, :sticky=>'nwe', :pady=>2, :padx=>3)
139f.grid_columnconfigure([0, 1, 2], :weight=>1, :uniform=>:yes)
140