Debian Codegraph @

demos/dynarows

raw
1 ## dynarows.tcl
2 ##
3 ## This demos shows the use of the validation mechanism of the table
4 ## and uses the table's cache (no -command or -variable) with a cute
5 ## dynamic row routine.
6 ##
7 ## [email protected]
8 ## Converted to perl/tk by John Cerney
9
10 use Tk;
11
12 use Tk::TableMatrix;
13
14 use Date::Parse;
15 use Date::Format;
16
17 my $top = MainWindow->new;
18
19 my $t = $top->Scrolled('TableMatrix', -rows => 2, -cols => 3, -cache => 1,
20 -selecttype => 'row',
21 -titlerows => 1, -titlecols => 1,
22 -height => 5,
23 -autoclear => 1,
24
25 );
26
27 $t->configure( -browsecommand => sub{
28 my ($index) = @_;
29
30 my $val = $t->get($index);
31 return unless $val;
32
33 my ($row,$col) = split(",",$index);
34
35 ## Entries in the last row are allowed to be empty
36 my $nrows = $t->cget(-rows);
37 if( ($row == ($nrows-1)) && $val eq ''){ return; };
38
39 return if( $row == 0 || $col == 0); #don't check the title row/cols
40 my $timenumber;
41 # try to parse date from value in cell
42 $timenumber = str2time($val);
43 if( !$timenumber || !$val){ # not a valid date:
44 print "'$val' is not a valid date\n";
45 $t->bell;
46 $t->activate($index);
47 $t->selectionClear('all');
48 $t->selectionSet('active');
49 $t->see('active');
50 }
51 else{
52
53 # Convert to a common date format
54 my $date;
55 $date = time2str("%m/%d/%Y",$timenumber);
56 $t->set($index,$date);
57 if( $row == ($nrows-1) ){
58 ## if this is the last row and both cols 1 && 2 are not empty
59 ## then add a row and redo configs
60 if( $t->get("$row,1") ne '' &&
61 $t->get("$row,2") ne ''){
62
63 $t->tagRow('', $row);
64 $t->set("$row,0", $row);
65 $t->configure( -rows => ++$nrows);
66 $t->tagRow('unset', ++$row);
67 $t->set("$row,0","*");
68 $t->see("$row,1");
69 $t->activate("$row,1");
70 }
71 }
72 }
73
74 });
75
76
77
78 $t->set("0,1" => "Begin", "0,2" => 'End', "1,0"=>"*");
79
80 # hideous Color definitions here:
81 $t->tagConfigure('unset', -fg => '#008811');
82 $t->tagConfigure('title', -fg => 'red');
83 $t->tagRow('unset', 1);
84
85 $t->colWidth( 0 => 3);
86
87 my $label = $top->Label(-text => "Dynamic Date Validated Rows");
88
89 $label->pack( -expand => 1, -fill => 'both');
90
91 $t->pack(-expand => 1, -fill => 'both');
92
93
94 # Bindings:
95 # Make the active area move after we press return:
96 # We Have to use class binding here so that we override
97 # the default return binding
98 $t->bind('Tk::TableMatrix','<Return>', sub{
99 my $r = $t->index('active', 'row');
100 my $c = $t->index('active', 'col');
101 if( $c == 2){
102 $t->activate(++$r.",1");
103 }
104 else{
105 $t->activate("$r,".++$c);
106 }
107 $t->see('active');
108 Tk->break;
109 });
110
111
112 # Make enter do the same thing as return:
113 $t->bind('<KP_Enter>', $t->bind('<Return>'));
114
115
116
117
118
119
120 Tk::MainLoop;