1#!/usr/bin/env ruby
2##
3## command.rb
4##
5## This demo shows the use of the table widget's -command options
6##
7## ( based on 'command.tcl' included source archive of tktable extension )
8##
9require 'tk'
10require 'tkextlib/tktable'
11
12# create the table
13data = TkVariable.new_hash
14rows = 10
15cols = 10
16
17# fill table variable
18((-(rows))..rows).each{|x|
19  ((-(cols))..cols).each{|y|
20    data[x,y] = "#{x} x #{y}"
21  }
22}
23
24lbl = TkLabel.new(:text=>"TkTable :command Example")
25cur_var = TkVariable.new
26current = TkLabel.new(:textvariable=>cur_var, :width=>5)
27ent_var = TkVariable.new
28entry = TkEntry.new(:textvariable=>ent_var)
29
30table = Tk::TkTable.new(:rows=>rows, :cols=>cols,
31                        :command=>[proc{|mode, cell, val|
32                          if (mode == :w)
33                            data[cell] = val
34                          else
35                            begin
36                              data[cell]  # exist
37                            rescue
38                              ''          # not exist
39                            end
40                          end
41                        }, '%i %C %s'],
42                        :width=>6, :height=>6,
43                        :titlerows=>1, :titlecols=>1,
44                        :roworigin=>-1, :colorigin=>-1,
45                        :rowstretchmode=>:last, :colstretchmode=>:last,
46                        :rowtagcommand=>proc{|row|
47                          row = Integer(row)
48                          (row>0 && row%2 == 1)? 'OddRow': ''
49                        },
50                        :coltagcommand=>proc{|col|
51                          col = Integer(col)
52                          (col>0 && col%2 == 1)? 'OddCol': ''
53                        },
54                        :selectmode=>:extended, :flashmode=>true,
55                        :rowstretch=>:unset, :colstretch=>:unset,
56                        :browsecommand=>[proc{|w, s|
57                          cur_var.value = s
58                          ent_var.value = w.get(s)
59                        }, '%W %S'],
60                        :validate=>true,
61                        :validatecommand=>proc{|e|
62                          ent_var.value = e.new_value; true
63                        })
64=begin
65                        :validatecommand=>[
66                          proc{|s|
67                            ent_var.value = s; true
68                          }, '%S'])
69=end
70
71sx = table.xscrollbar(TkScrollbar.new)
72sy = table.yscrollbar(TkScrollbar.new)
73
74entry.bind('Return', proc{|w| table.curvalue = w.value}, '%W')
75
76Tk.grid(lbl, '-', '-', :sticky=>:ew)
77Tk.grid(current, entry, '-', :sticky=>:ew)
78Tk.grid(table, '-', sy, :sticky=>:news)
79Tk.grid(sx, '-', :sticky=>:ew)
80
81Tk.root.grid_columnconfig(1, :weight=>1)
82Tk.root.grid_rowconfig(2, :weight=>1)
83
84table.tag_configure('OddRow', :bg=>'orange', :fg=>'purple')
85table.tag_configure('OddCol', :bg=>'brown', :fg=>'pink')
86
87puts "Table is #{table.path}"
88
89Tk.mainloop
90