OSDN Git Service

ruby-1.9.1-rc1
[splhack/AndroidRuby.git] / lib / ruby-1.9.1-rc1 / ext / tk / sample / tkextlib / tktable / command.rb
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 ##
9 require 'tk'
10 require 'tkextlib/tktable'
11
12 # create the table
13 data = TkVariable.new_hash
14 rows = 10
15 cols = 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
24 lbl = TkLabel.new(:text=>"TkTable :command Example")
25 cur_var = TkVariable.new
26 current = TkLabel.new(:textvariable=>cur_var, :width=>5)
27 ent_var = TkVariable.new
28 entry = TkEntry.new(:textvariable=>ent_var)
29
30 table = 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
71 sx = table.xscrollbar(TkScrollbar.new)
72 sy = table.yscrollbar(TkScrollbar.new)
73
74 entry.bind('Return', proc{|w| table.curvalue = w.value}, '%W')
75
76 Tk.grid(lbl, '-', '-', :sticky=>:ew)
77 Tk.grid(current, entry, '-', :sticky=>:ew)
78 Tk.grid(table, '-', sy, :sticky=>:news)
79 Tk.grid(sx, '-', :sticky=>:ew)
80
81 Tk.root.grid_columnconfig(1, :weight=>1)
82 Tk.root.grid_rowconfig(2, :weight=>1)
83
84 table.tag_configure('OddRow', :bg=>'orange', :fg=>'purple')
85 table.tag_configure('OddCol', :bg=>'brown', :fg=>'pink')
86
87 puts "Table is #{table.path}"
88
89 Tk.mainloop