File: tgdemo_edit

package info (click to toggle)
perl-tk 1%3A804.036%2Bdfsg1-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 35,284 kB
  • sloc: ansic: 349,560; perl: 52,292; sh: 12,678; makefile: 5,700; asm: 3,565; ada: 1,681; pascal: 1,082; cpp: 1,006; yacc: 883; cs: 879
file content (300 lines) | stat: -rw-r--r-- 7,216 bytes parent folder | download | duplicates (10)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
# Tix Demostration Program
#
# This sample program is structured in such a way so that it can be
# executed from the Tix demo program "widget": it must have a
# procedure called "RunSample". It should also have the "if" statment
# at the end of this file so that it can be run as a standalone
# program using tixwish.

require Tk;
require Tk::TixGrid;

# Demonstrates the use of editable entries in a Grid widget.
#

my %editgrid;

use strict;
use vars '$mw';
my $hasmw = (Tk::Exists($mw) ? 1 : 0);
$mw = Tk::MainWindow->new(-title=>'Edit TixGrid Demo') unless $hasmw;
RunSample($mw);
Tk::MainLoop() unless $hasmw;

sub RunSample
  {
    my ($w) = shift;

    $w->title("Doe Inc. Performance");
    #$w->geometry('640x300');

    $w->Label(-justify=>'left', -text =>
'The left column is calculated automatically. To calculate the right column,
press the "Calculate" button')
    	->pack(qw(-side top -anchor c -padx 3 -pady 3));

    # Create the buttons
    #
    my $f = $w->Frame(qw/-relief flat/)->pack(qw/-side right -fill y/);
    my $add  = $f->Button(-text=>"Add Row",  -width=>9, -command=>\&EditGrid_addRow);
    my $edit = $f->Button(-text=>"Edit",     -width=>9, -command=>\&EditGrid_edit);
    my $cal  = $f->Button(-text=>"Calculate",-width=>9, -command=>\&EditGrid_calculate);
    my $close= $f->Button(-text=>"Close",    -width=>9, -command=>[$w=>'destroy']);

    $add->pack(  qw/-side top    -padx 10/);
    $edit->pack( qw/-side top    -padx 10/);
    $cal->pack(  qw/-side top    -padx 10 -pady 2/);
    $close->pack(qw/-side bottom -padx 10/);

    # Create the grid and set options to make it editable.
    #
    my $grid = $w->Scrolled('TixGrid', -bd=>0)->
    			pack(qw/-expand yes -fill both -padx 3 -pady 3/);

    $grid->configure(
	-formatcmd     => [\&EditGrid_format,$grid],
	-editnotifycmd => \&EditGrid_editNotify,
	-editdonecmd   => \&EditGrid_editDone,
	-selectunit    => \&cell,
	-selectmode    => \&single);

    # Insert some initial data
    #
    $grid->set(0,1, -text=>"City #1");
    $grid->set(0,2, -text=>"City #2");
    $grid->set(0,3, -text=>"City #3");
    $grid->set(0,5, -text=>"Combined");

    $grid->set(2,0, -text=>"Population");
    $grid->set(4,0, -text=>"Avg. Income");

    $grid->set(qw/2 1 -text 125/);
    $grid->set(qw/2 2 -text  81/);
    $grid->set(qw/2 3 -text 724/);

    $grid->set(qw/4 1 -text 24432.12/);
    $grid->set(qw/4 2 -text 18290.24/);
    $grid->set(qw/4 3 -text 18906.34/);

    # Global data used by other EditGrid_ procedures.
    #
    %editgrid = (
		g	=> $grid,
    		top	=> 1,
		bot	=> 3,
		result	=> 5,
		);

    EditGrid_calPop();
    EditGrid_calIncome();
  }

# EditGrid_edit --
#
#	Prompts the user to edit a cell.
#
sub EditGrid_edit
  {
    my $grid = $editgrid{g};
    my @ent = $grid->anchor('get');
    unless (@ent)
      {
	#$grid->edit('set', @ent[0,1]);
	$grid->editSet(@ent[0,1]);
      }
  }

# EditGrid_addRow --
#
#	Adds a new row to the table.
#
sub EditGrid_addRow
  {
    my $grid =  $editgrid{g};

    #$grid->edit('apply');
    $grid->editApply;

    $grid->move('row', $editgrid{result}, $editgrid{result}, 1);
    $editgrid{bot}++;

    $editgrid{result} = $editgrid{bot} + 2;
    $grid->set(0, $editgrid{bot}, -text=>"City #$editgrid{bot}");
    $grid->set(2, $editgrid{bot}, -text=>0);
    $grid->set(4, $editgrid{bot}, -text=>0.0);

    EditGrid_calPop();
    EditGrid_calIncome();
}

# EditGrid_calPop --
#
#	Calculates the total population
#
sub EditGrid_calPop
  {
    my $grid = $editgrid{g};

    my $pop = 0;

    for (my $i = $editgrid{top}; $i <= $editgrid{bot}; $i++)
      {
	$pop += $grid->entrycget(2,$i,'-text');
      }

    $grid->set(2, $editgrid{result}, -text=>$pop);
}

# EditGrid_calIncome --
#
#	Calculates the average income.
#
sub EditGrid_calIncome
  {
    my $grid = $editgrid{g};

    my $income = 0;
    my $total_pop = 0;
    my ($pop, $inc);
    for (my $i = $editgrid{top}; $i <= $editgrid{bot}; $i++)
      {
	$pop = $grid->entrycget(2, $i, '-text');
	$inc = $grid->entrycget(4, $i, '-text');
	$income =  $income + ($pop+0) * $inc;
	$total_pop += $pop;
      }

    $grid->set(4, $editgrid{result}, -text=>($income/$total_pop));
 }

# EditGrid_calculate --
#
#	Recalculates both columns.
#
sub EditGrid_calculate
  {
    my $grid = $editgrid{g};

    #$grid->edit('apply');
    $grid->editApply;
    EditGrid_calIncome();
  }

# EditGrid_editNotify --
#
#	Returns true if an entry can be edited.
#
sub EditGrid_editNotify
  {
    my ($x, $y) = @_;
    my $grid = $editgrid{g};

    if ($x == 2 || $x == 4)
      {
	if ($y >= $editgrid{top} && $y <= $editgrid{bot})
          {
	    $editgrid{oldValue} = $grid->entrycget($x, $y, '-text');
	    return 1;
	  }
      }
    return 0;
  }

# EditGrid_editDone --
#
#	Gets called when the user is done editing an entry.
#
sub EditGrid_editDone
  {
    my ($x, $y) = @_;
    my $grid = $editgrid{g};

    if ($x == 2)
      {
	my $pop = $grid->entrycget($x, $y, '-text');
	my $val;
        eval { $val = sprintf("%d",$pop); } ;
        if ($@)
          {
	    $grid->entryconfigure($x, $y, -text=>$editgrid{oldValue});
	    # should be a custom warning.
	    $grid->TraceBack("$pop is not an valid integer. Try again");
          }
        else
          {
	    $grid->entryconfigure(4, $editgrid{result}, -text=>"-");
	    EditGrid_calPop();
	}
      }
    else
      {
	my $income = $grid->entrycget($x, $y, '-text');
	my $val;
        eval { $val = sprintf("%f",$income); } ;
        if ($@)
          {
	    $grid->entryconfigure($x, $y, -text=>$editgrid{oldValue});
	    # should be a custom warning.
	    $grid->TraceBack("$income is not an valid floating number. Try again");
          }
        else
          {
	    $grid->entryconfigure(4, $editgrid{result}, -text=>'-');
	  }
      }
  }

# EditGrid_format --
#
#	This command is called whenever the background of the grid
#	needs to be reformatted. The x1, y1, x2, y2 sprcifies the four
#	corners of the area that needs to be reformatted.
#
sub EditGrid_format
  {
    my ($w, $area, $x1, $y1, $x2, $y2) = @_;
    my $grid = $editgrid{g};

    my %bg = qw(
	s-margin gray65
	x-margin gray65
	y-margin gray65
	main     gray20
	);

    if ($area eq 'main')
      {
	foreach my $col (2, 4)
          {
	    $w->format('border', $col, 1, $col, $editgrid{bot},
		    qw/-relief flat -filled 1 -yon 1 -yoff 1
		       -bd 0 -bg #b0b0f0 -selectbackground #a0b0ff/);
	    $w->format('border', $col, 2, $col, $editgrid{bot},
		    qw/-relief flat -filled 1 -yon 1 -yoff 1
		    -bd 0 -bg #80b080 -selectbackground #80b0ff/);
	  }

	  $w->format('grid', $x1, $y1, $x2, $y2, -bordercolor=>$bg{$area},
		qw/-relief raised -bd 1 -filled 0 -bg red
		-xon 1 -yon 1 -xoff 0 -yoff 0 -anchor se/
		);
      }
    elsif ($area eq 'y-margin')
      {
	$w->format('border', $x1, $y1, $x2, $y2, -bg=>$bg{$area},
		qw/-filled 1 -relief raised -bd 1 -selectbackground gray80/);
      }
    else
      {
	$w->format('border', $x1, $y1, $x2, $y2, -bg=>$bg{$area},
		qw/-filled 1 -relief raised -bd 1 -selectbackground gray80/);
      }

#    case $area {
#	{main y-margin} {
#	    set y [expr $editgrid(bot) + 1]
#	    $w format border 0 $y 100 $y -bg black -filled 1 -bd 0
#	}
#     }
  }