1#!/usr/bin/env ruby
2#
3#  sample script of Tk::OptionObj
4#
5require "tk"
6
7optobj = Tk::OptionObj.new('foreground'=>'red', 'background'=>'black')
8
9f = TkFrame.new.pack(:side=>:left, :anchor=>:n, :padx=>5, :pady=>30)
10
11b1 = TkButton.new(f, :text=>'AAA').pack(:fill=>:x)
12b2 = TkButton.new(f, :text=>'BBB').pack(:fill=>:x)
13b3 = TkButton.new(f, :text=>'CCC').pack(:fill=>:x)
14
15optobj.assign( b1,
16              [ b2, 'configure',
17                { 'foreground'=>'background',
18                  'background'=>'foreground' } ],
19              [ b3, nil,
20                { 'foreground'=>'background',
21                  'activeforeground'=>nil,
22                  'background'=>['foreground', 'activeforeground'] } ] )
23
24optobj.update('activeforeground'=>'yellow')
25
26TkButton.new(f){
27  configure( optobj.assign(self) + {:text=>'DDD'} )
28  pack(:fill=>:x)
29}
30
31TkButton.new(f){
32  configure( optobj.assign([self, nil,
33                             {'foreground'=>'activeforeground',
34                              'background'=>'foreground',
35                              'activeforeground'=>'background'}]) \
36             + {:text=>'EEE', :relief=>:groove, :borderwidth=>5} )
37  pack(:fill=>:x)
38}
39
40optobj.notify  # To apply the convert_key ( 3rd element of widget info
41               # (that is, {'foreground'=>'activeforeground', ,,, } )
42               # of the 'EEE' button
43
44TkButton.new(f, :text=>'toggle',
45             :command=>proc{
46               fg = optobj['foreground']
47               bg = optobj['background']
48               optobj.configure('foreground'=>bg, 'background'=>fg)
49             }).pack(:fill=>:x, :pady=>10)
50
51TkButton.new(f, :text=>'exit',
52                :command=>proc{exit}).pack(:fill=>:x, :pady=>10)
53
54TkFrame.new{|f|
55  pack(:side=>:right, :expand=>true, :fill=>:both)
56  TkLabel.new(f, :text=>'source::').pack(:anchor=>:w)
57  TkFrame.new(f){|ff|
58    TkText.new(ff){
59      yscrollbar(TkScrollbar.new(ff){pack(:fill=>:y, :side=>:right)})
60      insert('end', File.read(__FILE__))
61      pack(:side=>:left, :expand=>true, :fill=>:both)
62    }
63    pack(:expand=>true, :fill=>:both)
64  }
65}
66
67Tk.mainloop
68