Debian Codegraph @

demos/rolodex

raw
1 #!/usr/bin/env perl
2 #
3 # This script was written as an entry in Tom LaStrange's rolodex benchmark.
4 # It creates something that has some of the look and feel of a rolodex program,# although it's lifeless and doesn't actually do the rolodex application.
5 #
6 # Tcl/Tk -> Perl translation by Stephen O. Lidie. [email protected] 95/01/16
7 # This needs a good re-write, but no time now (SOL), 96/01/25.
8
9 require 5.002;
10 use English;
11
12 use Tk;
13 use Tk::Dialog;
14
15 #------------------------------------------
16 # Phase 0: create the front end.
17 #------------------------------------------
18
19 $top = MainWindow->new;
20
21 $frame = $top->Frame(-relief => 'flat');
22 $frame->pack(-side => 'top', -fill => 'y', -anchor => 'center');
23
24 @names = ('', 'Name:', 'Address:', '', '', 'Home Phone:', 'Work Phone:', 'Fax:');
25 foreach $i (1..7) {
26
27 # Use symbolic/soft references to create the widget names at object time.
28 # Also, we need to keep a mapping of widget names to Perl/Tk references
29 # for the online help to work.
30
31 ($f,$l,$e) = ("frame_${i}", "frame_${i}_l", "frame_${i}_e");
32 ${$f} = $frame->Frame();
33 ${$f}->pack(-side => 'top', -pady => '2', -anchor => 'e');
34
35 ${$l} = ${$f}->Label(-text => $names[$i], -anchor => 'e');
36 ${$e} = ${$f}->Entry(-width => '30', -relief => 'sunken');
37 ${$e}->pack(-side => 'right');
38 ${$l}->pack(-side => 'right');
39 $ref_to_name{$e} = ${$e}; # map Entry widget to its Perl/Tk reference
40 }
41
42 $buttons = $top->Frame();
43 $buttons->pack(-side => 'bottom', -pady => '2', -anchor => 'center');
44 $buttons_clear = $buttons->Button(-text => 'Clear');
45 $buttons_add = $buttons->Button(-text => 'Add');
46 $buttons_search = $buttons->Button(-text => 'Search');
47 $buttons_delete = $buttons->Button(-text => 'Delete ...');
48 $buttons_clear->pack(-side => 'left', -padx => '2');
49 $buttons_add->pack(-side => 'left', -padx => '2');
50 $buttons_search->pack(-side => 'left', -padx => '2');
51 $buttons_delete->pack(-side => 'left', -padx => '2');
52
53 #------------------------------------------
54 # Phase 1: Add menus, dialog boxes
55 #------------------------------------------
56
57 $menu = $top->Frame(-relief => 'raised', -borderwidth => '1');
58 $menu->pack(-before => $frame, -side => 'top', -fill => 'x');
59
60 $menu_file = $menu->Menubutton(-text => 'File', -underline => 0);
61 $ref_to_name{'menu_file'} = $menu_file;
62 $menu_file->command(-label => 'load ...', -command => \&fileAction,
63 -underline => 0);
64 $menu_file->command(-label => 'Exit', -command => \&exit, -underline => 0);
65 $menu_file->pack(-side => 'left');
66
67 $menu_help = $menu->Menubutton(-text => 'Help', -underline => '0');
68 $menu_help->pack(-side => 'right');
69
70 # Make all the Dialog objects.
71
72 $DELACT = $top->Dialog(
73 -title => 'Confirm Action',
74 -text => 'Are you sure?',
75 -bitmap => 'questhead',
76 -default_button => 'Cancel',
77 -buttons => ['OK', 'Cancel'],
78 );
79 $FILACT = $top->Dialog(
80 -title => 'File Selection',
81 -bitmap => 'info',
82 -text => 'This is a dummy file selection dialog box, which is used because there isn\'t a good file selection dialog built into Tk yet.',
83 );
84 $help = $top->Dialog(-title => 'Rolodex Help',-justify => 'left');
85
86 sub deleteAction {
87 &clearAction if $DELACT->Show eq 'OK';
88 }
89 $buttons_delete->configure(-command => \&deleteAction);
90
91 sub fileAction {
92 print STDERR "dummy file name\n" if $FILACT->Show eq 'OK';
93 }
94
95 #------------------------------------------
96 # Phase 3: Print contents of card
97 #------------------------------------------
98
99 sub addAction {
100 foreach $i (1..7) {
101 ($f,$l,$e) = ("frame_${i}", "frame_${i}_l", "frame_${i}_e");
102 printf STDERR "%-12s %s\n", $names[$i], ${$e}->get;
103 }
104 }
105 $buttons_add->configure(-command => \&addAction);
106
107 #------------------------------------------
108 # Phase 4: Miscellaneous other actions
109 #------------------------------------------
110
111 sub clearAction {
112 foreach $i (1..7) {
113 ($f,$l,$e) = ("frame_${i}", "frame_${i}_l", "frame_${i}_e");
114 ${$e}->delete('0', 'end');
115 }
116 }
117 $buttons_clear->configure(-command => \&clearAction);
118
119 sub fillCard {
120 &clearAction;
121 my(@text) = ('', 'John Ousterhout', 'CS Division, Department of EECS',
122 'University of California', 'Berkeley, CA 94720',
123 'private', '510-642-0865', '510-642-5775');
124 foreach $i (1..7) {
125 ($f,$l,$e) = ("frame_${i}", "frame_${i}_l", "frame_${i}_e");
126 ${$e}->insert('0', $text[$i]);
127 }
128 }
129 $buttons_search->configure(-command => sub {&addAction; &fillCard});
130
131 #----------------------------------------------------
132 # Phase 5: Accelerators, mnemonics, command-line info
133 #----------------------------------------------------
134
135 $buttons_clear->configure(-text => 'Clear Ctrl+C');
136 $top->bind('<Control-c>' => \&clearAction);
137 $buttons_add->configure(-text => 'Add Ctrl+A');
138 $top->bind('<Control-a>' => \&addAction);
139 $buttons_search->configure(-text => 'Search Ctrl+S');
140 $top->bind('<Control-s>' => sub {&addAction; &fillCard});
141 $buttons_delete->configure(-text => 'Delete... Ctrl+D');
142 $top->bind('<Control-d>' => \&deleteAction);
143
144 $menu_file_m = $menu_file->cget(-menu);
145 $menu_file_m->entryconfigure(1, -accelerator => 'Ctrl+F');
146 $top->bind('<Control-f>' => \&fileAction);
147 $menu_file_m->entryconfigure(2, -accelerator => 'Ctrl+Q');
148 $top->bind('<Control-q>' => sub {exit});
149
150 # We do this rather than simply $frame_1_e->focus to eliminate the Perl
151 # "single use" warning message.
152
153 ${"frame_1_e"}->focus;
154
155 #----------------------------------------------------
156 # Phase 6: help
157 #----------------------------------------------------
158
159 sub Help {
160 my($topic, $x, $y) = @_;
161 if ($topic eq '') {return;};
162 while (defined $helpCmds{$topic}) {
163 $topic = $helpCmds{$topic};
164 }
165 for $widget (keys %ref_to_name) {
166 $topic = $widget if $topic eq $ref_to_name{$widget};
167 }
168 if (defined $helpTopics{$topic}) {
169 $msg = $helpTopics{$topic};
170 } else {
171 $msg = 'Sorry, but no help is available for this topic';
172 }
173 $help->Subwidget('message')->configure(
174 -text => "Information on $topic:\n\n$msg");
175 $help->Show;
176 }
177
178 $top->bind('<Any-F1>' => sub {
179 my $w = shift;
180 my $e = $w->XEvent;
181 &Help($w->containing($e->X, $e->Y), $e->X, $e->Y);
182 });
183 $top->bind('<Any-Help>' => sub {
184 my $w = shift;
185 my $e = $w->XEvent;
186 &Help($w->containing($e->X, $e->Y), $e->X, $e->Y);
187 });
188
189 # Help text and commands follow:
190
191 $helpTopics{'menu_file'} = 'This is the "file" menu. It can be used to invoke some overall operations on the rolodex ' .
192 'applications, such as loading a file or exiting.';
193
194 $helpTopics{'frame_1_e'} = 'In this field of the rolodex entry you should type the person\'s name';
195 $helpTopics{'frame_2_e'} = 'In this field of the rolodex entry you should type the first line of the person\'s address';
196 $helpTopics{'frame_3_e'} = 'In this field of the rolodex entry you should type the second line of the person\'s address';
197 $helpTopics{'frame_4_e'} = 'In this field of the rolodex entry you should type the third line of the person\'s address';
198 $helpTopics{'frame_5_e'} = 'In this field of the rolodex entry you should type the person\'s home phone number, or ' .
199 '"private" if the person doesn\'t want his or her number publicized';
200 $helpTopics{'frame_6_e'} = 'In this field of the rolodex entry you should type the person\'s work phone number';
201 $helpTopics{'frame_7_e'} = 'In this field of the rolodex entry you should type the phone number for the person\'s FAX machine';
202
203 $helpCmds{'frame_1_l'} = 'frame_1_e';
204 $helpCmds{'frame_2_l'} = 'frame_2_e';
205 $helpCmds{'frame_3_l'} = 'frame_3_e';
206 $helpCmds{'frame_4_l'} = 'frame_4_e';
207 $helpCmds{'frame_5_l'} = 'frame_5_e';
208 $helpCmds{'frame_6_l'} = 'frame_6_e';
209 $helpCmds{'frame_7_l'} = 'frame_7_e';
210
211 $helpTopics{'context'} = 'Unfortunately, this application doesn\'t support context-sensitive help in the usual way, because ' .
212 'when this demo was written Tk didn\'t have a grab mechanism and this is needed for context-sensitive help. ' .
213 'Instead, you can achieve much the same effect by simply moving the mouse over the window you\'re curious about ' .
214 'and pressing the Help or F1 keys. You can do this anytime.';
215 $helpTopics{'help'} = 'This application provides only very crude help. Besides the entries in this menu, you can get help ' .
216 'on individual windows by moving the mouse cursor over the window and pressing the Help or F1 keys.';
217 $helpTopics{'window'} = 'This window is a dummy rolodex application created as part of Tom LaStrange\'s toolkit benchmark. ' .
218 'It doesn\'t really do anything useful except to demonstrate a few features of the Tk toolkit.';
219 $helpTopics{'keys'} = "The following accelerator keys are defined for this application (in addition to those already " .
220 "available for the entry windows):\n\nCtrl+A:\t\tAdd\nCtrl+C:\t\tClear\nCtrl+D:\t\tDelete\nCtrl+F:\t\tEnter file " .
221 "name\nCtrl+Q:\t\tExit application (quit)\nCtrl+S:\t\tSearch (dummy operation)";
222 $helpTopics{'version'} = 'This is version 1.0.';
223
224 # Entries in "Help" menu
225
226 $menu_help->command(-label => 'On Context...', -command => [\&Help, 'context'], -underline => 3);
227 $menu_help->command(-label => 'On Help...', -command => [\&Help, 'help'], -underline => 3);
228 $menu_help->command(-label => 'On Window...', -command => [\&Help, 'window'], -underline => 3);
229 $menu_help->command(-label => 'On Keys...', -command => [\&Help, 'keys'], -underline => 3);
230 $menu_help->command(-label => 'On Version...', -command => [\&Help, 'version'], -underline => 3);
231
232 MainLoop;