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