summaryrefslogtreecommitdiff
path: root/trunk/ext/tk/sample/tkextlib/bwidget/basic.rb
blob: c1521651fdec233a9a723be08ee75d09b6469a19 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#
#  basic demo  ---  called from demo.rb
#
unless Object.const_defined?('DemoVar')
  fail RuntimeError, "This is NOT a stand alone script. This script is called from 'demo.rb'. "
end

module DemoBasic
  @@var   = TkVariable.new_hash
  @@after = nil
  @@count = 0

  def self.create(nb)
    frame = nb.insert('end', 'demoBasic', :text=>'Basic')
    topf  = TkFrame.new(frame)

    titf1 = Tk::BWidget::TitleFrame.new(topf, :text=>'Label')
    titf2 = Tk::BWidget::TitleFrame.new(topf, :text=>'Entry')

    titf3 = Tk::BWidget::TitleFrame.new(frame, :text=>'Button and ArrowButton')

    _label(titf1.get_frame)
    _entry(titf2.get_frame)
    _button(titf3.get_frame)

    Tk.pack(titf1, titf2, :side=>:left, :fill=>:both, :padx=>4, :expand=>true)
    topf.pack(:pady=>2, :fill=>:x)
    titf3.pack(:pady=>2, :padx=>4, :fill=>:x)
  end

  def self._label(parent)
    lab = Tk::BWidget::Label.new(parent, :text=>'This is a Label widget', 
                                 :helptext=>'Label widget')
    chk = TkCheckbutton.new(parent, :text=>'Disabled', 
                            :variable=>@@var.ref(lab, 'state'), 
                            :onvalue=>'disabled', :offvalue=>'normal', 
                            :command=>proc{lab[:state] = @@var[lab, 'state']})
    lab.pack(:anchor=>:w, :pady=>4)
    chk.pack(:anchor=>:w)
  end

  def self._entry(parent)
    ent = Tk::BWidget::Entry.new(parent, :text=>'Press enter', 
                                 :helptext=>'Entry widtet', 
                                 :command=>proc{
                                   @@var['entcmd'] = 'command called'
                                   Tk.after(500, proc{@@var['entcmd'] = ''})
                                 })

    chk1 = TkCheckbutton.new(parent, :text=>'Disabled', 
                             :variable=>@@var.ref(ent, 'state'), 
                             :onvalue=>'disabled', :offvalue=>'normal', 
                             :command=>proc{ent.state = @@var[ent, 'state']})

    chk2 = TkCheckbutton.new(parent, :text=>'Non editable', 
                             :variable=>@@var.ref(ent, 'editable'), 
                             :onvalue=>false, :offvalue=>true, 
                             :command=>proc{
                               ent.editable = @@var[ent, 'editable']
                             })

    lab = TkLabel.new(parent, :textvariable=>@@var.ref('entcmd'), 
                      :foreground=>'red')

    ent.pack(:pady=>4, :anchor=>:w)
    Tk.pack(chk1, chk2, :anchor=>:w)
    lab.pack(:pady=>4)
  end

  def self._button(parent)
    frame = TkFrame.new(parent)
    but  = Tk::BWidget::Button.new(frame, :text=>'Press me!', 
                                   :repeatdelay=>300, 
                                   :command=>proc{_butcmd('command')}, 
                                   :helptext=>'This is a Button widget')

    sep1 = Tk::BWidget::Separator.new(frame, :orient=>:vertical)
    arr1 = Tk::BWidget::ArrowButton.new(frame, :type=>:button, 
                :width=>25, :height=>25, :repeatdelay=>300, 
                :command=>proc{_butcmd('command')}, 
                :helptext=>"This is an ArrowButton widget\nof type button")

    sep2 = Tk::BWidget::Separator.new(frame, :orient=>:vertical)
    arr2 = Tk::BWidget::ArrowButton.new(frame, :type=>:arrow, 
                :width=>25, :height=>25, :relief=>:sunken, 
                :ipadx=>0, :ipady=>0, :repeatdelay=>300, 
                :command=>proc{_butcmd('command')}, 
                :helptext=>"This is an ArrowButton widget\nof type arrow")

    but.pack(:side=>:left, :padx=>4)
    sep1.pack(:side=>:left, :padx=>4, :fill=>:y)
    arr1.pack(:side=>:left, :padx=>4)
    sep2.pack(:side=>:left, :padx=>4, :fill=>:y)
    arr2.pack(:side=>:left, :padx=>4)
    frame.pack

    Tk::BWidget::Separator.new(parent, 
                               :orient=>:horizontal).pack(:fill=>:x, :pady=>10)

    labf1 = Tk::BWidget::LabelFrame.new(parent, :text=>'Command', 
                                        :side=>:top, :anchor=>:w, 
                                        :relief=>:sunken, :borderwidth=>1)
    subf = labf1.get_frame
    chk1 = TkCheckbutton.new(subf, :text=>'Disabled', 
                             :variable=>@@var.ref('bstate'), 
                             :onvalue=>'disabled', :offvalue=>'normal', 
                             :command=>proc{_bstate(@@var['bstate'], 
                                                     but, arr1, arr2)})
    chk2 = TkCheckbutton.new(subf, :text=>"Use armcommand/\ndisarmcommand", 
                             :variable=>@@var.ref('barmcmd'), 
                             :command=>proc{_barmcmd(@@var['barmcmd'], 
                                                     but, arr1, arr2)})
    Tk.pack(chk1, chk2, :anchor=>:w)

    label = TkLabel.new(parent, :textvariable=>@@var.ref('butcmd'), 
                        :foreground=>'red').pack(:side=>:bottom, :pady=>4)

    labf2 = Tk::BWidget::LabelFrame.new(parent, :text=>'Direction', 
                                        :side=>:top, :anchor=>:w, 
                                        :relief=>:sunken, :borderwidth=>1)
    subf = labf2.get_frame
    @@var['bside'] = :top
    [:top, :left, :bottom, :right].each{|dir|
      TkRadiobutton.new(subf, :text=>"#{dir} arrow", 
                        :variable=>@@var.ref('bside'), :value=>dir, 
                        :command=>proc{_bside(@@var['bside'], arr1, arr2)}
                        ).pack(:anchor=>:w)
    }

    labf3 = Tk::BWidget::LabelFrame.new(parent, :text=>'Relief', 
                                        :side=>:top, :anchor=>:w, 
                                        :relief=>:sunken, :borderwidth=>1)
    subf = labf3.get_frame
    @@var['brelief'] = :raised
    [ %w(raised sunken ridge groove), %w(flat solid link)].each{|lrelief|
      f = TkFrame.new(subf)
      lrelief.each{|relief|
        TkRadiobutton.new(f, :text=>relief, 
                          :variable=>@@var.ref('brelief'), :value=>relief, 
                          :command=>proc{
                            _brelief(@@var['brelief'], but, arr1, arr2)
                          }).pack(:anchor=>:w)
      }
      f.pack(:side=>:left, :padx=>2, :anchor=>:n)
    }
    Tk.pack(labf1, labf2, labf3, :side=>:left, :fill=>:y, :padx=>4)
  end

  def self._bstate(state, but, arr1, arr2)
    [but, arr1, arr2].each{|b| b[:state] = state}
  end

  def self._brelief(relief, but, arr1, arr2)
    but[:relief] = relief
    if relief.to_s != 'link'
      [arr1, arr2].each{|arr| arr[:relief] = relief}
    end
  end

  def self._bside(side, *args)
    args.each{|arr| arr[:dir] = side}
  end

  def self._barmcmd(value, but, arr1, arr2)
    if TkComm.bool(value)
      but.configure(:armcommand=>proc{_butcmd('arm')}, 
                    :disarmcommand=>proc{_butcmd('disarm')}, 
                    :command=>'')
      [arr1, arr2].each{|arr|
        arr.configure(:armcommand=>proc{_butcmd('arm')}, 
                      :disarmcommand=>proc{_butcmd('disarm')}, 
                      :command=>'')
      }
    else
      but.configure(:armcommand=>'', :disarmcommand=>'', 
                    :command=>proc{_butcmd('command')})
      [arr1, arr2].each{|arr|
        arr.configure(:armcommand=>'', :disarmcommand=>'', 
                      :command=>proc{_butcmd('command')})
      }
    end
  end

  def self._butcmd(reason)
    unless @@after
      @@after = TkTimer.new(500, 1, proc{@@var['butcmd'] = ''})
    end
    @@after.stop
    if (reason == 'arm')
      @@count += 1
      @@var['butcmd'] = "#{reason} command called (#{@@count})"
    else
      @@count = 0
      @@var['butcmd'] = "#{reason} command called"
    end
    @@after.start
  end
end