1#!/usr/bin/env ruby
2##
3## basic.rb
4##
5## This demo shows the basic use of the table widget
6##
7## ( based on 'basic.tcl' included source archive of tktable extension )
8##
9require 'tk'
10require 'tkextlib/tktable'
11
12ary  = TkVariable.new_hash
13rows = 8
14cols = 8
15
16# fill table variable
17((-(rows))..rows).each{|x|
18  ((-(cols))..cols).each{|y|
19    ary[x,y] = "r#{x},c#{y}"
20  }
21}
22
23lbl = TkLabel.new(:text=>"TkTable v1 Example")
24
25table = Tk::TkTable.new(:rows=>rows, :cols=>cols, :variable=>ary,
26                        :width=>6, :height=>6,
27                        :titlerows=>1, :titlecols=>2,
28                        :roworigin=>-1, :colorigin=>-2,
29                        :rowstretchmode=>:last, :colstretchmode=>:last,
30                        :rowtagcommand=>proc{|row|
31                          row = Integer(row)
32                          (row>0 && row%2 == 1)? 'OddRow': ''
33                        },
34                        :coltagcommand=>proc{|col|
35                          col = Integer(col)
36                          (col>0 && col%2 == 1)? 'OddCol': ''
37                        },
38                        :selectmode=>:extended, :sparsearray=>false)
39
40sx = table.xscrollbar(TkScrollbar.new)
41sy = table.yscrollbar(TkScrollbar.new)
42
43btn = TkButton.new(:text=>'Exit', :command=>proc{exit})
44
45Tk.grid(lbl, '-', :sticky=>:ew)
46Tk.grid(table, sy, :sticky=>:news)
47Tk.grid(sx, :sticky=>:ew)
48Tk.grid(btn, :sticky=>:ew, :columnspan=>2)
49
50Tk.root.grid_columnconfig(0, :weight=>1)
51Tk.root.grid_rowconfig(1, :weight=>1)
52
53table.tag_configure('OddRow', :bg=>'orange', :fg=>'purple')
54table.tag_configure('OddCol', :bg=>'brown', :fg=>'pink')
55
56table.set_width([-2, 7], [-1, 7], [1, 5], [2, 8], [4, 14])
57
58puts "Table is #{table.path} with array #{(table['variable'])}"
59
60Tk.mainloop
61