Debian Codegraph @

TixGrid/tgdemo_edit

raw
1 # Tix Demostration Program
2 #
3 # This sample program is structured in such a way so that it can be
4 # executed from the Tix demo program "widget": it must have a
5 # procedure called "RunSample". It should also have the "if" statment
6 # at the end of this file so that it can be run as a standalone
7 # program using tixwish.
8
9 require Tk;
10 require Tk::TixGrid;
11
12 # Demonstrates the use of editable entries in a Grid widget.
13 #
14
15 my %editgrid;
16
17 use strict;
18 use vars '$mw';
19 my $hasmw = (Tk::Exists($mw) ? 1 : 0);
20 $mw = Tk::MainWindow->new(-title=>'Edit TixGrid Demo') unless $hasmw;
21 RunSample($mw);
22 Tk::MainLoop() unless $hasmw;
23
24 sub RunSample
25 {
26 my ($w) = shift;
27
28 $w->title("Doe Inc. Performance");
29 #$w->geometry('640x300');
30
31 $w->Label(-justify=>'left', -text =>
32 'The left column is calculated automatically. To calculate the right column,
33 press the "Calculate" button')
34 ->pack(qw(-side top -anchor c -padx 3 -pady 3));
35
36 # Create the buttons
37 #
38 my $f = $w->Frame(qw/-relief flat/)->pack(qw/-side right -fill y/);
39 my $add = $f->Button(-text=>"Add Row", -width=>9, -command=>\&EditGrid_addRow);
40 my $edit = $f->Button(-text=>"Edit", -width=>9, -command=>\&EditGrid_edit);
41 my $cal = $f->Button(-text=>"Calculate",-width=>9, -command=>\&EditGrid_calculate);
42 my $close= $f->Button(-text=>"Close", -width=>9, -command=>[$w=>'destroy']);
43
44 $add->pack( qw/-side top -padx 10/);
45 $edit->pack( qw/-side top -padx 10/);
46 $cal->pack( qw/-side top -padx 10 -pady 2/);
47 $close->pack(qw/-side bottom -padx 10/);
48
49 # Create the grid and set options to make it editable.
50 #
51 my $grid = $w->Scrolled('TixGrid', -bd=>0)->
52 pack(qw/-expand yes -fill both -padx 3 -pady 3/);
53
54 $grid->configure(
55 -formatcmd => [\&EditGrid_format,$grid],
56 -editnotifycmd => \&EditGrid_editNotify,
57 -editdonecmd => \&EditGrid_editDone,
58 -selectunit => \&cell,
59 -selectmode => \&single);
60
61 # Insert some initial data
62 #
63 $grid->set(0,1, -text=>"City #1");
64 $grid->set(0,2, -text=>"City #2");
65 $grid->set(0,3, -text=>"City #3");
66 $grid->set(0,5, -text=>"Combined");
67
68 $grid->set(2,0, -text=>"Population");
69 $grid->set(4,0, -text=>"Avg. Income");
70
71 $grid->set(qw/2 1 -text 125/);
72 $grid->set(qw/2 2 -text 81/);
73 $grid->set(qw/2 3 -text 724/);
74
75 $grid->set(qw/4 1 -text 24432.12/);
76 $grid->set(qw/4 2 -text 18290.24/);
77 $grid->set(qw/4 3 -text 18906.34/);
78
79 # Global data used by other EditGrid_ procedures.
80 #
81 %editgrid = (
82 g => $grid,
83 top => 1,
84 bot => 3,
85 result => 5,
86 );
87
88 EditGrid_calPop();
89 EditGrid_calIncome();
90 }
91
92 # EditGrid_edit --
93 #
94 # Prompts the user to edit a cell.
95 #
96 sub EditGrid_edit
97 {
98 my $grid = $editgrid{g};
99 my @ent = $grid->anchor('get');
100 unless (@ent)
101 {
102 #$grid->edit('set', @ent[0,1]);
103 $grid->editSet(@ent[0,1]);
104 }
105 }
106
107 # EditGrid_addRow --
108 #
109 # Adds a new row to the table.
110 #
111 sub EditGrid_addRow
112 {
113 my $grid = $editgrid{g};
114
115 #$grid->edit('apply');
116 $grid->editApply;
117
118 $grid->move('row', $editgrid{result}, $editgrid{result}, 1);
119 $editgrid{bot}++;
120
121 $editgrid{result} = $editgrid{bot} + 2;
122 $grid->set(0, $editgrid{bot}, -text=>"City #$editgrid{bot}");
123 $grid->set(2, $editgrid{bot}, -text=>0);
124 $grid->set(4, $editgrid{bot}, -text=>0.0);
125
126 EditGrid_calPop();
127 EditGrid_calIncome();
128 }
129
130 # EditGrid_calPop --
131 #
132 # Calculates the total population
133 #
134 sub EditGrid_calPop
135 {
136 my $grid = $editgrid{g};
137
138 my $pop = 0;
139
140 for (my $i = $editgrid{top}; $i <= $editgrid{bot}; $i++)
141 {
142 $pop += $grid->entrycget(2,$i,'-text');
143 }
144
145 $grid->set(2, $editgrid{result}, -text=>$pop);
146 }
147
148 # EditGrid_calIncome --
149 #
150 # Calculates the average income.
151 #
152 sub EditGrid_calIncome
153 {
154 my $grid = $editgrid{g};
155
156 my $income = 0;
157 my $total_pop = 0;
158 my ($pop, $inc);
159 for (my $i = $editgrid{top}; $i <= $editgrid{bot}; $i++)
160 {
161 $pop = $grid->entrycget(2, $i, '-text');
162 $inc = $grid->entrycget(4, $i, '-text');
163 $income = $income + ($pop+0) * $inc;
164 $total_pop += $pop;
165 }
166
167 $grid->set(4, $editgrid{result}, -text=>($income/$total_pop));
168 }
169
170 # EditGrid_calculate --
171 #
172 # Recalculates both columns.
173 #
174 sub EditGrid_calculate
175 {
176 my $grid = $editgrid{g};
177
178 #$grid->edit('apply');
179 $grid->editApply;
180 EditGrid_calIncome();
181 }
182
183 # EditGrid_editNotify --
184 #
185 # Returns true if an entry can be edited.
186 #
187 sub EditGrid_editNotify
188 {
189 my ($x, $y) = @_;
190 my $grid = $editgrid{g};
191
192 if ($x == 2 || $x == 4)
193 {
194 if ($y >= $editgrid{top} && $y <= $editgrid{bot})
195 {
196 $editgrid{oldValue} = $grid->entrycget($x, $y, '-text');
197 return 1;
198 }
199 }
200 return 0;
201 }
202
203 # EditGrid_editDone --
204 #
205 # Gets called when the user is done editing an entry.
206 #
207 sub EditGrid_editDone
208 {
209 my ($x, $y) = @_;
210 my $grid = $editgrid{g};
211
212 if ($x == 2)
213 {
214 my $pop = $grid->entrycget($x, $y, '-text');
215 my $val;
216 eval { $val = sprintf("%d",$pop); } ;
217 if ($@)
218 {
219 $grid->entryconfigure($x, $y, -text=>$editgrid{oldValue});
220 # should be a custom warning.
221 $grid->TraceBack("$pop is not an valid integer. Try again");
222 }
223 else
224 {
225 $grid->entryconfigure(4, $editgrid{result}, -text=>"-");
226 EditGrid_calPop();
227 }
228 }
229 else
230 {
231 my $income = $grid->entrycget($x, $y, '-text');
232 my $val;
233 eval { $val = sprintf("%f",$income); } ;
234 if ($@)
235 {
236 $grid->entryconfigure($x, $y, -text=>$editgrid{oldValue});
237 # should be a custom warning.
238 $grid->TraceBack("$income is not an valid floating number. Try again");
239 }
240 else
241 {
242 $grid->entryconfigure(4, $editgrid{result}, -text=>'-');
243 }
244 }
245 }
246
247 # EditGrid_format --
248 #
249 # This command is called whenever the background of the grid
250 # needs to be reformatted. The x1, y1, x2, y2 sprcifies the four
251 # corners of the area that needs to be reformatted.
252 #
253 sub EditGrid_format
254 {
255 my ($w, $area, $x1, $y1, $x2, $y2) = @_;
256 my $grid = $editgrid{g};
257
258 my %bg = qw(
259 s-margin gray65
260 x-margin gray65
261 y-margin gray65
262 main gray20
263 );
264
265 if ($area eq 'main')
266 {
267 foreach my $col (2, 4)
268 {
269 $w->format('border', $col, 1, $col, $editgrid{bot},
270 qw/-relief flat -filled 1 -yon 1 -yoff 1
271 -bd 0 -bg #b0b0f0 -selectbackground #a0b0ff/);
272 $w->format('border', $col, 2, $col, $editgrid{bot},
273 qw/-relief flat -filled 1 -yon 1 -yoff 1
274 -bd 0 -bg #80b080 -selectbackground #80b0ff/);
275 }
276
277 $w->format('grid', $x1, $y1, $x2, $y2, -bordercolor=>$bg{$area},
278 qw/-relief raised -bd 1 -filled 0 -bg red
279 -xon 1 -yon 1 -xoff 0 -yoff 0 -anchor se/
280 );
281 }
282 elsif ($area eq 'y-margin')
283 {
284 $w->format('border', $x1, $y1, $x2, $y2, -bg=>$bg{$area},
285 qw/-filled 1 -relief raised -bd 1 -selectbackground gray80/);
286 }
287 else
288 {
289 $w->format('border', $x1, $y1, $x2, $y2, -bg=>$bg{$area},
290 qw/-filled 1 -relief raised -bd 1 -selectbackground gray80/);
291 }
292
293 # case $area {
294 # {main y-margin} {
295 # set y [expr $editgrid(bot) + 1]
296 # $w format border 0 $y 100 $y -bg black -filled 1 -bd 0
297 # }
298 # }
299 }
300