~ubuntu-branches/ubuntu/hardy/ruby1.8/hardy-updates

« back to all changes in this revision

Viewing changes to ext/tk/sample/tkextlib/tktable/dynarows.rb

  • Committer: Bazaar Package Importer
  • Author(s): akira yamada
  • Date: 2007-03-13 22:11:58 UTC
  • mfrom: (1.1.5 upstream)
  • Revision ID: james.westby@ubuntu.com-20070313221158-h3oql37brlaf2go2
Tags: 1.8.6-1
* new upstream version, 1.8.6.
* libruby1.8 conflicts with libopenssl-ruby1.8 (< 1.8.6) (closes: #410018)
* changed packaging style to cdbs from dbs.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
##
 
11
require 'tk'
 
12
require 'tkextlib/tktable'
 
13
 
 
14
def 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
 
52
end
 
53
 
 
54
 
 
55
lbl = TkLabel.new(:text=>"Dynamic Date Validated Rows")
 
56
 
 
57
table = 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
                        ])
 
65
table.set([0,1], 'Begin', [0,2], 'End', [1,0], '*')
 
66
table.tag_configure('unset', :fg=>'#008811')
 
67
table.tag_configure('title', :fg=>'red')
 
68
table.tag_row('unset', 1)
 
69
table.set_width(0,3)
 
70
 
 
71
sx = table.xscrollbar(TkScrollbar.new)
 
72
sy = table.yscrollbar(TkScrollbar.new)
 
73
 
 
74
Tk.grid(lbl, '-', :sticky=>:ew)
 
75
Tk.grid(table, sy, :sticky=>:news)
 
76
Tk.grid(sx, :sticky=>:ew)
 
77
 
 
78
Tk.root.grid_columnconfig(0, :weight=>1)
 
79
Tk.root.grid_rowconfig(1, :weight=>1)
 
80
 
 
81
rtn_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
 
 
96
table.bind('Return', rtn_proc, '%W')
 
97
table.bind('KP_Enter', rtn_proc, '%W')
 
98
 
 
99
Tk.mainloop