-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTkCount
More file actions
executable file
·265 lines (218 loc) · 7.25 KB
/
TkCount
File metadata and controls
executable file
·265 lines (218 loc) · 7.25 KB
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
#!/usr/bin/perl -w
#
use strict;
use English;
use Tk;
use Tk::Xrm;
use Tk::DialogBox;
use Date::Manip;
use Config::IniFiles;
use subs qw(handler show_usage update cleanup_and_die);
use subs qw(file_menuitems help_menuitems);
use POSIX qw(strftime);
use vars qw($opt_t $opt_v $opt_u $opt_x);
use Getopt::Std;
#-------------------------------------------------------------------
# General Variables and runtime information.
#-------------------------------------------------------------------
use vars qw($rc $update_time $message $message_fmt $Rev $RunDate $DirName);
use vars qw($ProgName $DefSection);
use vars qw($now $target $delta);
my $FONT = '-*-Helvetica-Medium-R-Normal--*-140-*-*-*-*-*-*';
$message_fmt = "There are XX days until we leave on vacation!";
$now = ParseDate("today");
$target = $now;
#Widgets
use vars qw($main $top $textbox $header $w_update $w_quit $listbox);
$RunDate = strftime '%Y/%m/%d %H:%M:%S', localtime;
$Rev = ( split( ' ', '$Revision: 2 $', 3 ) )[1];
$0 =~ m!(.*)/!;
$ProgName = $';
$DirName = $1;
$DirName = '.' unless $DirName;
#--------------------------------------------------------------
# General utility subroutines
#--------------------------------------------------------------
sub show_usage {
print "$ProgName:\n";
print " Options:\n";
print " -u <update> : Update time in seconds (default is 1 sec).\n";
print " -x : Verbose degug information.\n";
print " -t : Text mode, just report the countdown time.\n";
print "\n";
print "$ProgName $Rev\t\t$RunDate\n\n";
print " In the window inputing:\n";
print " Control-C : Exits the program.\n";
print "\n";
exit 0;
}
sub cleanup_and_die {
my $msg = $_[0];
die "$msg";
}
sub update {
# Update the counter every 1 second.
#print &UnixDate("today","It is now %T on %b %e, %Y.");
# => "It is now 13:24:08 on Feb 3, 1996."
$now = ParseDate("today");
$delta = DateCalc( $now, $target );
#print "Now=$now, Target=$target, Delta=$delta\n";
#my $datestr = Delta_Format($delta, 1, "Delta is %dh days, and %hd hours\n");
#my $datestr = Delta_Format($delta, 1, "Delta is %dt days\n");
#print "$datestr\n";
# load date into message...
$message = Delta_Format( $delta, 3, $message_fmt );
$opt_v && print "Update... message='$message'\n";
if ( defined($top) ) {
$top->after( $update_time, \&update );
}
}
sub file_menuitems {
# Create the menu items for the File menu.
my ( $motif, $bisque ) = ( 1, 0 );
my $new_image_format = 'png';
[ [qw/cascade ~New -accelerator Ctrl-n/],
[qw/command ~Open -accelerator Ctrl-o/],
[qw/command ~Save -accelerator Ctrl-s/],
'',
[qw/command ~Options -accelerator Ctrl-i/],
'',
[ qw/command ~Update -accelerator Ctrl-u -command/ => \&update ],
[ qw/command ~Quit -accelerator Ctrl-q -command/ => \&exit ],
];
} # end file_menuitems
sub help_menuitems {
[ [ 'command', 'Version', -command => sub { print "Version\n" } ],
'',
[ 'command', 'About', -command => sub { print "About\n" } ],
];
}
sub LoadRC {
# read and load config file
my $cfg_file = "$ENV{HOME}/." . $ProgName . "rc";
$opt_v && print " ... checking for config file at \$HOME: $cfg_file\n";
if ( -f "$cfg_file" ) {
$rc = new Config::IniFiles( -file => $cfg_file );
}
else {
$cfg_file = "$ENV{HOME}/bin/" . $ProgName . ".rc";
$opt_v && print " ... checking for config file in cwd: $cfg_file\n";
if ( -f "$cfg_file" ) {
$rc = new Config::IniFiles( -file => $cfg_file );
}
else {
$cfg_file = $ProgName . ".rc";
$opt_v
&& print " ... checking for config file in cwd: $cfg_file\n";
if ( -f "$cfg_file" ) {
$rc = new Config::IniFiles( -file => $cfg_file );
}
else {
print "Cannot find ini file $cfg_file to load\n";
}
}
}
if ( defined($rc) ) {
use vars qw ($rc_update $rc_message $rc_date);
my @Sections = $rc->Sections;
$opt_v
&& print "The defined sections are: "
. join( " ", @Sections ) . "\n";
for my $sect (@Sections) {
if ( !defined($DefSection) ) {
$DefSection = $sect;
$opt_v
&& print " ... $sect set as the default\n";
}
my @parms = $rc->Parameters($sect);
$opt_v
&& print " ... the parameters are: "
. join( " ", @parms ) . "\n";
}
if ( defined( $rc_update = $rc->val( $DefSection, 'update' ) ) ) {
$update_time = $rc_update;
$opt_v
&& print "update set from ini file: $update_time\n";
}
if ( defined( $rc_message = $rc->val( $DefSection, 'message' ) ) ) {
$message_fmt = $rc_message;
$message_fmt =~ s/"//g;
$opt_v
&& print "message format set from ini file: $message_fmt\n";
}
if ( defined( $rc_date = $rc->val( $DefSection, 'date' ) ) ) {
$opt_v
&& print
" ... parsing of target date from file: $rc_date\n";
$rc_date =~ s/"//g;
$target = ParseDateString($rc_date);
if ( !defined($target) ) {
die "Bad parsing of target date from file: $rc_date\n";
}
$opt_v
&& print "Target date set from ini file: $target\n";
}
}
}
sub SetFont {
}
#----------------------------------------------------------------------------------
#
# Main program
#
#
# parse options...
#
my @SAVEDARGV = @ARGV;
if ( &getopts('tvxu:') == 0 ) {
&show_usage;
}
#print "# $ProgName $Rev\t\t$RunDate\n\n";
$update_time = 1000;
LoadRC();
if ( defined($opt_u) ) {
if ( $opt_u !~ /\d+/ ) {
die "Invalid number specified for the update time: $opt_u\n";
}
$update_time = int( $opt_u * 1000 );
}
if ( $update_time == 0 ) {
print "Bad update time conversion\n";
}
elsif ( $update_time < 100 ) {
print "Setting update time to minimum of 0.1 seconds\n";
$update_time = 100;
}
unless ( defined($opt_t) ) {
$top = MainWindow->new;
$top->bind( '<Control-c>' => \&exit );
$top->bind( '<Control-u>' => \&update );
$top->title('TkCount');
$top->iconname('TkCount');
$top->geometry('700x50');
$top->focus;
# Create the menubar.
my $menubar = $top->Frame(qw/-relief raised -borderwidth 2/);
$menubar->pack(qw/-fill x/);
# Create the menubar's menubuttons.
my $file = $menubar->Menubutton( qw/-text File -underline 0/,
-menuitems => file_menuitems );
my $help = $menubar->Menubutton( qw/-text Help -underline 0/,
-menuitems => help_menuitems );
# In Unix the Help menubutton is right justified.
$file->pack(qw/-side left/);
$help->pack(qw/-side right/);
# Define the countdown message
$textbox = $top->Label( -textvariable => \$message )->pack(
-side => 'bottom',
-fill => 'y'
);
$textbox->configure( -font => [qw/arial 18 medium/] );
}
&update;
if ( defined($opt_t) ) {
print "$message\n";
}
else {
MainLoop;
}