1#!/usr/bin/env ruby
2##
3## debug.rb
4##
5## This demo uses most features of the table widget
6##
7## ( based on 'debug.tcl' included source archive of tktable extension )
8##
9require 'tk'
10require 'tkextlib/tktable'
11
12# create the table
13ary  = TkVariable.new_hash
14rows = 25
15cols = 20
16
17# fill table variable
18((-(rows))..rows).each{|x|
19  ((-(cols))..cols).each{|y|
20    ary[x,y] = "r#{x},c#{y}"
21  }
22}
23
24lbl = TkLabel.new(:text=>"TkTable v2 Example")
25
26table = Tk::TkTable.new(:rows=>rows, :cols=>cols, :variable=>ary,
27                        :width=>6, :height=>6,
28                        :titlerows=>1, :titlecols=>2,
29                        :roworigin=>-5, :colorigin=>-2,
30                        :coltagcommand=>proc{|col|
31                          col = Integer(col)
32                          (col>0 && col%2 == 1)? 'OddCol': ''
33                        },
34                        :selectmode=>:extended, :flashmode=>true,
35                        :rowstretch=>:unset, :colstretch=>:unset,
36                        :selecttitles=>false, :drawmode=>:single)
37
38sx = table.xscrollbar(TkScrollbar.new)
39sy = table.yscrollbar(TkScrollbar.new)
40
41btn = TkButton.new(:text=>'Exit', :command=>proc{exit})
42
43Tk.grid(lbl, '-', :sticky=>:ew)
44Tk.grid(table, sy, :sticky=>:news)
45Tk.grid(sx, :sticky=>:ew)
46Tk.grid(btn, :sticky=>:ew, :columnspan=>2)
47
48Tk.root.grid_columnconfig(0, :weight=>1)
49Tk.root.grid_rowconfig(1, :weight=>1)
50
51table.tag_configure('OddCol', :bg=>'brown', :fg=>'pink')
52table.tag_configure('title',  :bg=>'red',   :fg=>'green', :relief=>:sunken)
53table.tag_configure('dis',    :state=>:disabled)
54
55first = table[:colorigin]
56%w(n s e w nw ne sw se c).each_with_index{|anchor, idx|
57  table.tag_configure(anchor, :anchor=>anchor)
58  table.tag_row(anchor, idx)
59  table.set([idx,first], anchor)
60}
61courier = TkFont.new(:family=>'Courier', :size=>10)
62table.tag_configure('s', :font=>courier, :justify=>:center)
63
64logo = TkPhotoImage.new(:file=>File.join(File.dirname(File.expand_path(__FILE__)), 'tcllogo.gif'))
65table.tag_configure('logo', :image=>logo, :showtext=>true)
66table.tag_cell('logo', [1,2], [2,3], [4,1])
67table.tag_cell('dis', [2,1], [1,-1], [3,0])
68table.set_width([-2,8], [-1,9], [0, 12], [4, 14])
69
70table.set([1,1], "multi-line\ntext\nmight be\ninteresting",
71          [3,2], "more\nmulti-line\nplaying\n",
72          [2,2], "null\0byte")
73
74# This is in the row span
75l = TkLabel.new(table, :text=>'Window s', :bg=>'yellow')
76table.window_configure([6,0], :sticky=>:s, :window=>l)
77
78# This is in the row titles
79l = TkLabel.new(table, :text=>'Window ne', :bg=>'yellow')
80table.window_configure([4,-1], :sticky=>:ne, :window=>l)
81
82# This will get swallowed by a span
83l = TkLabel.new(table, :text=>'Window ew', :bg=>'yellow')
84table.window_configure([5,3], :sticky=>:ew, :window=>l)
85
86# This is in the col titles
87l = TkLabel.new(table, :text=>'Window news', :bg=>'yellow')
88table.window_configure([-5,1], :sticky=>:news, :window=>l)
89
90l = TkLabel.new(table.winfo_parent, :text=>'Sibling l', :bg=>'orange')
91table.window_configure([5,1], :sticky=>:news, :window=>l)
92
93if table.span_list.empty?
94  table.set_spans([-1,-2], [0,3], [1,2], [0,5], [3,2], [2,2], [6,0], [4,0])
95end
96
97puts "Table is #{table.path} with array #{(table['variable'])}"
98
99# table.postscript(:file=>'out.ps', :first=>:origin, :last=>[2,2])
100
101Tk.mainloop
102