1#!/usr/bin/env ruby
2require 'tk'
3require 'tkextlib/bwidget'
4
5module DemoVar
6  @_wfont    = nil
7  @notebook  = nil
8  @mainframe = nil
9  @status    = TkVariable.new
10  @prgtext   = TkVariable.new
11  @prgindic  = TkVariable.new
12  @font      = nil
13  @font_name = nil
14  @toolbar1  = TkVariable.new(true)
15  @toolbar2  = TkVariable.new(true)
16end
17class << DemoVar
18  attr_accessor :_wfont, :notebook, :mainframe, :font, :font_name
19  attr_reader   :status, :prgtext, :prgindic, :toolbar1, :toolbar2
20end
21
22class BWidget_Demo
23  DEMODIR = File.dirname(File.expand_path(__FILE__))
24
25  %w(manager basic select dnd tree tmpldlg).each{|f|
26    require File.join(DEMODIR, f << '.rb')
27  }
28
29  def initialize
30    TkOption.add('*TitleFrame.l.font', 'helvetica 11 bold italic')
31
32    root = TkRoot.new(:title=>'BWidget demo')
33    root.withdraw
34
35    _create
36
37    Tk::BWidget.place(root, 0, 0, :center)
38    root.deiconify
39    root.raise
40    root.focus(true)
41
42    root.geometry(root.geometry)
43  end
44
45  def _create
46    DemoVar.prgtext.value = 'Please wait while loading font...'
47    DemoVar.prgindic.value = -1
48
49    intro = _create_intro
50
51    Tk.update
52
53    Tk::BWidget::SelectFont.load_font
54
55    descmenu = [
56      '&File', 'all', 'file', 0, [
57        ['command', 'E&xit', [], 'Exit BWidget demo', [],
58          {:command=>proc{exit}}]
59      ],
60      '&Options', 'all', 'options', 0, [
61        ['checkbutton', 'Toolbar &1', ['all', 'option'],
62          'Show/hide toolbar 1', [],
63          { :variable=>DemoVar.toolbar1,
64            :command=>proc{
65              DemoVar.mainframe.show_toolbar(0, DemoVar.toolbar1.value)
66            }
67          }
68        ],
69        ['checkbutton', 'Toolbar &2', ['all', 'option'],
70          'Show/hide toolbar 2', [],
71          { :variable=>DemoVar.toolbar2,
72            :command=>proc{
73              DemoVar.mainframe.show_toolbar(1, DemoVar.toolbar2.value)
74            }
75          }
76        ]
77      ]
78    ]
79
80    DemoVar.prgtext.value = 'Creating MainFrame...'
81    DemoVar.prgindic.value = 0
82
83    DemoVar.mainframe = Tk::BWidget::MainFrame.new(
84                                :menu=>descmenu,
85                                :textvariable=>DemoVar.status,
86                                :progressvar=>DemoVar.prgindic
87                        )
88
89    # toobar 1 creation
90    DemoVar.prgindic.numeric += 1
91
92    DemoVar.mainframe.add_toolbar{|tb1|
93      Tk::BWidget::ButtonBox.new(tb1, :spacing=>0, :padx=>1, :pady=>1){|bbox|
94        add(:image=>Tk::BWidget::Bitmap.new('new'),
95            :highlightthickness=>0, :takefocus=>0, :relief=>:link,
96            :borderwidth=>1, :padx=>1, :pady=>1,
97            :command=>proc{puts 'select "Create a new file" icon'},
98            :helptext=>"Create a new file")
99
100        add(:image=>Tk::BWidget::Bitmap.new('open'),
101            :highlightthickness=>0, :takefocus=>0, :relief=>:link,
102            :borderwidth=>1, :padx=>1, :pady=>1,
103            :command=>proc{puts 'select "Open an existing file" icon'},
104            :helptext=>"Open an existing file")
105
106        add(:image=>Tk::BWidget::Bitmap.new('save'),
107            :highlightthickness=>0, :takefocus=>0, :relief=>:link,
108            :borderwidth=>1, :padx=>1, :pady=>1,
109            :command=>proc{puts 'select "Save file" icon'},
110            :helptext=>"Save file")
111
112        pack(:side=>:left, :anchor=>:w)
113      }
114
115      Tk::BWidget::Separator.new(tb1, :orient=>:vertical){
116        pack(:side=>:left, :fill=>:y, :padx=>4, :anchor=>:w)
117      }
118
119      DemoVar.prgindic.numeric += 1
120
121      Tk::BWidget::ButtonBox.new(tb1, :spacing=>0, :padx=>1, :pady=>1){|bbox|
122        add(:image=>Tk::BWidget::Bitmap.new('cut'),
123            :highlightthickness=>0, :takefocus=>0, :relief=>:link,
124            :borderwidth=>1, :padx=>1, :pady=>1,
125            :command=>proc{puts 'select "Cut selection" icon'},
126            :helptext=>"Cut selection")
127
128        add(:image=>Tk::BWidget::Bitmap.new('copy'),
129            :highlightthickness=>0, :takefocus=>0, :relief=>:link,
130            :borderwidth=>1, :padx=>1, :pady=>1,
131            :command=>proc{puts 'select "Copy selection" icon'},
132            :helptext=>"Copy selection")
133
134        add(:image=>Tk::BWidget::Bitmap.new('paste'),
135            :highlightthickness=>0, :takefocus=>0, :relief=>:link,
136            :borderwidth=>1, :padx=>1, :pady=>1,
137            :command=>proc{puts 'select "Paste selection" icon'},
138            :helptext=>"Paste selection")
139
140        pack(:side=>:left, :anchor=>:w)
141      }
142    }
143
144    # toolbar 2 creation
145    DemoVar.prgindic.numeric += 1
146
147    tb2 = DemoVar.mainframe.add_toolbar
148    DemoVar._wfont = Tk::BWidget::SelectFont::Toolbar.new(tb2,
149                       :command=>proc{update_font(DemoVar._wfont[:font])}
150                     )
151    DemoVar.font = DemoVar._wfont[:font]
152    DemoVar._wfont.pack(:side=>:left, :anchor=>:w)
153
154    DemoVar.mainframe.add_indicator(
155      :text=>"BWidget #{Tk::BWidget.package_version}"
156    )
157    DemoVar.mainframe.add_indicator(:textvariable=>'tk_patchLevel')
158
159    # NoteBook creation
160    DemoVar.notebook = Tk::BWidget::NoteBook.new(DemoVar.mainframe.get_frame)
161
162    DemoVar.prgtext.value = "Creating Manager..."
163    DemoVar.prgindic.numeric += 1
164    DemoManager.create(DemoVar.notebook)
165
166    DemoVar.prgtext.value = "Creating Basic..."
167    DemoVar.prgindic.numeric += 1
168    DemoBasic.create(DemoVar.notebook)
169
170    DemoVar.prgtext.value = "Creating Select..."
171    DemoVar.prgindic.numeric += 1
172    DemoSelect.create(DemoVar.notebook)
173
174    DemoVar.prgtext.value = "Creating Dialog..."
175    DemoVar.prgindic.numeric += 1
176    DemoDialog.create(DemoVar.notebook)
177
178    DemoVar.prgtext.value = "Creating Drag and Drop..."
179    DemoVar.prgindic.numeric += 1
180    DemoDnD.create(DemoVar.notebook)
181
182    DemoVar.prgtext.value = "Creating Tree..."
183    DemoVar.prgindic.numeric += 1
184    DemoTree.create(DemoVar.notebook)
185
186    DemoVar.prgtext.value = "Done"
187    DemoVar.prgindic.numeric += 1
188
189    DemoVar.notebook.compute_size
190    DemoVar.notebook.pack(:fill=>:both, :expand=>true, :padx=>4, :pady=>4)
191    DemoVar.notebook.raise(DemoVar.notebook.get_page(0))
192
193    DemoVar.mainframe.pack(:fill=>:both, :expand=>true)
194
195    Tk.update_idletasks
196
197    intro.destroy
198  end
199
200  def update_font(newfont)
201    root = Tk.root
202    root[:cursor] = 'watch'
203    if newfont != '' && DemoVar.font != newfont
204      DemoVar._wfont[:font] = newfont
205      DemoVar.notebook[:font] = newfont
206      DemoVar.font = newfont
207    end
208    root[:cursor] = ''
209  end
210
211  def _create_intro
212    top = TkToplevel.new(:relief=>:raised, :borderwidth=>2)
213    top.withdraw
214    top.overrideredirect(true)
215
216    ximg  = TkLabel.new(top, :bitmap=>"@#{File.join(DEMODIR,'x1.xbm')}",
217                        :foreground=>'grey90', :background=>'white')
218    bwimg = TkLabel.new(ximg, :bitmap=>"@#{File.join(DEMODIR,'bwidget.xbm')}",
219                        :foreground=>'grey90', :background=>'white')
220    frame = TkFrame.new(ximg, :background=>'white')
221    TkLabel.new(frame, :text=>'Loading demo',
222                :background=>'white', :font=>'times 8').pack
223    TkLabel.new(frame, :textvariable=>DemoVar.prgtext,
224                :background=>'white', :font=>'times 8', :width=>35).pack
225    Tk::BWidget::ProgressBar.new(frame, :width=>50, :height=>10,
226                                 :background=>'white',
227                                 :variable=>DemoVar.prgindic,
228                                 :maximum=>10).pack
229    frame.place(:x=>0, :y=>0, :anchor=>:nw)
230    bwimg.place(:relx=>1, :rely=>1, :anchor=>:se)
231    ximg.pack
232    Tk::BWidget.place(top, 0, 0, :center)
233    top.deiconify
234
235    top
236  end
237end
238
239module DemoVar
240  Demo = BWidget_Demo.new
241end
242
243Tk.mainloop
244