summaryrefslogtreecommitdiff
path: root/ext/tk/sample/tkextlib/bwidget/tmpldlg.rb
blob: da2c9678c0fab4a9e2fcf951ac6f0762de93ccf7 (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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#
#  templdlg 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 DemoDialog
  @@tmpl      = TkVariable.new_hash
  @@msg       = TkVariable.new_hash
  @@msgdlg    = nil
  @@progmsg   = TkVariable.new
  @@progval   = TkVariable.new
  @@progdlg   = nil
  @@resources = TkVariable.new('en')

  def self.create(nb)
    frame = nb.insert('end', 'demoDlg', :text=>'Dialog')

    titf1 = Tk::BWidget::TitleFrame.new(frame, :text=>'Resources')
    titf2 = Tk::BWidget::TitleFrame.new(frame, :text=>'Template Dialog')
    titf3 = Tk::BWidget::TitleFrame.new(frame, :text=>'Message Dialog')
    titf4 = Tk::BWidget::TitleFrame.new(frame, :text=>'Other dialog')

    subf = titf1.get_frame
    cmd = proc{ TkOption.read_file(File.join(Tk::BWidget::LIBRARY, 'lang',
                                             @@resources.value + '.rc')) }
    Tk.pack(TkRadiobutton.new(subf, :text=>'English', :value=>'en',
                              :variable=>@@resources, :command=>cmd),
            TkRadiobutton.new(subf, :text=>'French', :value=>'fr',
                              :variable=>@@resources, :command=>cmd),
            TkRadiobutton.new(subf, :text=>'German', :value=>'de',
                              :variable=>@@resources, :command=>cmd),
            :side=>:left)

    _tmpldlg(titf2.get_frame)
    _msgdlg(titf3.get_frame)
    _stddlg(titf4.get_frame)

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

  def self._tmpldlg(parent)
    @@tmpl['side'] = :bottom
    @@tmpl['anchor'] = :c

    labf1 = Tk::BWidget::LabelFrame.new(parent, :text=>'Button side',
                                        :side=>:top, :anchor=>:w,
                                        :relief=>:sunken, :borderwidth=>1)
    subf = labf1.get_frame
    Tk.pack(TkRadiobutton.new(subf, :text=>'Bottom', :value=>:bottom,
                              :variable=>@@tmpl.ref('side'), :anchor=>:w),
            TkRadiobutton.new(subf, :text=>'Left', :value=>:left,
                              :variable=>@@tmpl.ref('side'), :anchor=>:w),
            TkRadiobutton.new(subf, :text=>'Right', :value=>:right,
                              :variable=>@@tmpl.ref('side'), :anchor=>:w),
            TkRadiobutton.new(subf, :text=>'Top', :value=>:top,
                              :variable=>@@tmpl.ref('side'), :anchor=>:w),
            :fill=>:x, :anchor=>:w)

    labf2 = Tk::BWidget::LabelFrame.new(parent, :text=>'Button anchor',
                                        :side=>:top, :anchor=>:w,
                                        :relief=>:sunken, :borderwidth=>1)
    subf = labf2.get_frame
    Tk.pack(TkRadiobutton.new(subf, :text=>'North', :value=>:n,
                              :variable=>@@tmpl.ref('anchor'), :anchor=>:w),
            TkRadiobutton.new(subf, :text=>'West', :value=>:w,
                              :variable=>@@tmpl.ref('anchor'), :anchor=>:w),
            TkRadiobutton.new(subf, :text=>'East', :value=>:e,
                              :variable=>@@tmpl.ref('anchor'), :anchor=>:w),
            TkRadiobutton.new(subf, :text=>'South', :value=>:s,
                              :variable=>@@tmpl.ref('anchor'), :anchor=>:w),
            TkRadiobutton.new(subf, :text=>'Center', :value=>:c,
                              :variable=>@@tmpl.ref('anchor'), :anchor=>:w),
            :fill=>:x, :anchor=>:w)

    sep = Tk::BWidget::Separator.new(parent, :orient=>:horizontal)
    button = TkButton.new(parent, :text=>'Show', :command=>proc{_show_tmpldlg})

    button.pack(:side=>:bottom)
    sep.pack(:side=>:bottom, :fill=>:x, :pady=>10)
    Tk.pack(labf1, labf2, :side=>:left, :padx=>4, :anchor=>:n)
  end

  def self._msgdlg(parent)
    @@msg['type'] = 'ok'
    @@msg['icon'] = 'info'

    labf1 = Tk::BWidget::LabelFrame.new(parent, :text=>'Type', :side=>:top,
                                        :anchor=>:w, :relief=>:sunken,
                                        :borderwidth=>1)
    subf = labf1.get_frame
    Tk.pack(TkRadiobutton.new(subf, :text=>'Ok', :value=>'ok',
                              :variable=>@@msg.ref('type'), :anchor=>:w),
            TkRadiobutton.new(subf, :text=>'Ok, Cancel', :value=>'okcancel',
                              :variable=>@@msg.ref('type'), :anchor=>:w),
            TkRadiobutton.new(subf, :text=>'Retry, Cancel',
                              :value=>'retrycancel',
                              :variable=>@@msg.ref('type'), :anchor=>:w),
            TkRadiobutton.new(subf, :text=>'Yes, No', :value=>'yesno',
                              :variable=>@@msg.ref('type'), :anchor=>:w),
            TkRadiobutton.new(subf, :text=>'Yes, No, Cancel',
                              :value=>'yesnocancel',
                              :variable=>@@msg.ref('type'), :anchor=>:w),
            TkRadiobutton.new(subf, :text=>'Abort, Retry, Ignore',
                              :value=>'abortretryignore',
                              :variable=>@@msg.ref('type'), :anchor=>:w),
            :fill=>:x, :anchor=>:w)

    Tk.pack(TkRadiobutton.new(subf, :text=>'User', :value=>'user',
                              :variable=>@@msg.ref('type'), :anchor=>:w),
            Tk::BWidget::Entry.new(subf, :textvariable=>@@msg.ref('buttons')),
            :side=>:left)

    labf2 = Tk::BWidget::LabelFrame.new(parent, :text=>'Icon', :side=>:top,
                                        :anchor=>:w, :relief=>:sunken,
                                        :borderwidth=>1)
    subf = labf2.get_frame
    Tk.pack(TkRadiobutton.new(subf, :text=>'Information', :value=>'info',
                              :variable=>@@msg.ref('icon'), :anchor=>:w),
            TkRadiobutton.new(subf, :text=>'Question',    :value=>'question',
                              :variable=>@@msg.ref('icon'), :anchor=>:w),
            TkRadiobutton.new(subf, :text=>'Warning',     :value=>'warning',
                              :variable=>@@msg.ref('icon'), :anchor=>:w),
            TkRadiobutton.new(subf, :text=>'Error',       :value=>'error',
                              :variable=>@@msg.ref('icon'), :anchor=>:w),
            :fill=>:x, :anchor=>:w)

    sep = Tk::BWidget::Separator.new(parent, :orient=>:horizontal)
    button = TkButton.new(parent, :text=>'Show', :command=>proc{_show_msgdlg})

    button.pack(:side=>:bottom)
    sep.pack(:side=>:bottom, :fill=>:x, :pady=>10)
    Tk.pack(labf1, labf2, :side=>:left, :padx=>4, :anchor=>:n)
  end

  def self._stddlg(parent)
    Tk.pack(TkButton.new(parent, :text=>'Select a color '){|w|
              command(proc{DemoDialog._show_color(w)})
            },
            TkButton.new(parent, :text=>'Font selector dialog',
                         :command=>proc{_show_fontdlg}),
            TkButton.new(parent, :text=>'Progression dialog',
                         :command=>proc{_show_progdlg}),
            TkButton.new(parent, :text=>'Password dialog',
                         :command=>proc{_show_passdlg}),
            :side=>:left, :padx=>5, :anchor=>:w)
  end

  def self._show_color(w)
    dlg = Tk::BWidget::SelectColor.new(w, :color=>w.background)
    color = dlg.menu([:below, w])
    unless color.empty?
      w.background(color)
    end
  end

  def self._show_tmpldlg
    dlg = Tk::BWidget::Dialog.new(:relative=>Tk.root, :modal=>:local,
                                  :separator=>true, :title=>'Template dialog',
                                  :side=>@@tmpl['side'],
                                  :anchor=>@@tmpl['anchor'],
                                  :default=>0, :cancel=>1)
    dlg.add('name'=>'ok')
    dlg.add('name'=>'cancel')
    TkMessage.new(dlg.get_frame, :text=>"Template\nDialog", :justify=>:center,
                  :anchor=>:c, :width=>80).pack(:fill=>:both, :expand=>true,
                                                :padx=>100, :pady=>100)
    dlg.draw
    dlg.destroy
  end

  def self._show_msgdlg
    @@msgdlg.destroy if @@msgdlg
    @@msgdlg = Tk::BWidget::MessageDlg.new(:relative=>Tk.root,
                                           :message=>'Message for MessageBox',
                                           :type=>@@msg['type'],
                                           :icon=>@@msg['icon'],
                                           :buttons=>@@msg['buttons'])
    @@msgdlg.create
  end

  def self._show_fontdlg
    font = Tk::BWidget::SelectFont.new(:relative=>Tk.root,
                                       :font=>DemoVar.font).create
    DemoVar::Demo.update_font(font)
  end

  def self._show_progdlg
    @@progmsg.value = "Compute in progress..."
    @@progval.value = 0

    @@progdlg = Tk::BWidget::ProgressDlg.new(:relative=>Tk.root,
                                             :title=>'Wait...',
                                             :type=>'infinite', :width=>20,
                                             :textvariable=>@@progmsg,
                                             :variable=>@@progval,
                                             :stop=>'Stop') {
      command(proc{self.destroy})
      create
    }
    _update_progdlg
  end

  def self._update_progdlg
    TkTimer.new(20, -1, proc{
                  if @@progdlg && @@progdlg.winfo_exist?
                    @@progval.value = 2
                  else
                    stop
                  end
                }).start
  end

  def self._show_passdlg
    Tk::BWidget::PasswdDlg.new(:relative=>Tk.root).create
  end
end