1#!/usr/bin/env ruby 2## 3## dynarows.rb 4## 5## This demos shows the use of the validation mechanism of the table 6## and uses the table's cache (no -command or -variable) with a cute 7## dynamic row routine. 8## 9## ( based on 'dynarows.tcl' included source archive of tktable extension ) 10## 11require 'tk' 12require 'tkextlib/tktable' 13 14def table_validate(w, idx) 15 return unless idx =~ /^(\d+),(\d+)$/ 16 row = Integer($1) 17 col = Integer($2) 18 val = w.get(idx) 19 20 [w, idx] 21 nrows = w[:rows] 22 return if row == nrows - 1 && val == '' 23 24 begin 25 time = Tk.tk_call('clock', 'scan', val) 26 date = [] 27 Tk.tk_call('clock', 'format', time, 28 :format=>'%m %d %Y').split(' ').each{|item| 29 date << item.sub(/^\s*0*/,'') 30 } 31 w.set(idx, date.join('/')) 32 if row == nrows - 1 33 if w.get([row,1]) != '' && w.get([row,2]) != '' 34 w.tag_row_reset(row) 35 w.set([row,0], row) 36 nrows += 1 37 row += 1 38 w.configure(:rows=>nrows) 39 w.tag_row('unset', row) 40 w.set([row,0], '*') 41 w.see([row,1]) 42 w.activate([row,1]) 43 end 44 end 45 rescue 46 Tk.bell 47 w.activate(idx) 48 w.selection_clear_all 49 w.selection_set(:active) 50 w.see(:active) 51 end 52end 53 54 55lbl = TkLabel.new(:text=>"Dynamic Date Validated Rows") 56 57table = Tk::TkTable.new(:rows=>2, :cols=>3, :cache=>1, :selecttype=>:row, 58 :titlerows=>1, :titlecols=>1, :height=>5, 59 :colstretch=>:unset, :rowstretch=>:unset, 60 :autoclear=>true, 61 :browsecommand=>[ 62 proc{|w,s| table_validate(w, s)}, 63 '%W %s' 64 ]) 65table.set([0,1], 'Begin', [0,2], 'End', [1,0], '*') 66table.tag_configure('unset', :fg=>'#008811') 67table.tag_configure('title', :fg=>'red') 68table.tag_row('unset', 1) 69table.set_width(0,3) 70 71sx = table.xscrollbar(TkScrollbar.new) 72sy = table.yscrollbar(TkScrollbar.new) 73 74Tk.grid(lbl, '-', :sticky=>:ew) 75Tk.grid(table, sy, :sticky=>:news) 76Tk.grid(sx, :sticky=>:ew) 77 78Tk.root.grid_columnconfig(0, :weight=>1) 79Tk.root.grid_rowconfig(1, :weight=>1) 80 81rtn_proc = proc{|w| 82 r = w.row_index(:active) 83 c = w.col_index(:active) 84 85 if c == 2 86 r += 1 87 w.activate([r,1]) 88 else 89 c += 1 90 w.activate([r,c]) 91 end 92 w.see(:active) 93 Tk.callback_break 94} 95 96table.bind('Return', rtn_proc, '%W') 97table.bind('KP_Enter', rtn_proc, '%W') 98 99Tk.mainloop 100