-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwidgetTclpTk
668 lines (561 loc) · 22.3 KB
/
widgetTclpTk
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
#!/usr/local/bin/perl
use 5.008;
use warnings;
use strict;
use Config;
use Tcl::pTk qw/:perlTk/;
use lib Tcl::pTk->findINC( 'demos/widget_lib' );
#use Tk::widgets qw/ DialogBox ErrorDialog LabEntry ROText /;
use WidgetDemo;
use subs qw/
build_about_dialog demos invoke lsearch
see_code see_vars show_stat view_widget
/;
use vars qw/ $MW $FONT $WIDTRIB /;
use vars qw/ $CODE $CODE_RERUN $CODE_TEXT $VARS $VIEW $VIEW_TEXT /;
use vars qw/ $ALIGN $BRAKES $LIGHTS $OIL $SOBER $TRANS $WIPERS /;
use vars qw/ $COLOR $FONT_STYLE $POINT_SIZE $DEMO_FILE %DEMO_DESCRIPTION /;
eval {
# Try to enable system DPI awareness for sharper UI on Windows
# https://www.perlmonks.org/?node_id=11101747
if ($^O eq 'MSWin32') {
require Win32::API;
# See https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-setprocessdpiaware
my $SetProcessDPIAware = Win32::API::More->new('User32', 'BOOL SetProcessDPIAware()');
$SetProcessDPIAware->Call();
}
};
$MW = MainWindow->new;
$MW->configure(-menu => my $menubar = $MW->Menu);
{
package WidgetWrap;
@WidgetWrap::ISA = qw/Tcl::pTk::Widget::MainWindow/;
# This magic conspires with widget's AUTOLOAD subroutine to make user
# contributed demonstrations that don't use WidgetDemo embed properly.
# The trick works because widget creates a superclass of Tk::MainWindow
# which invokes WidgetDemo() implicitly. You loose if you bypass the
# inheritance mechanism and call Tk::MainWindow directly.
sub new {
my ($name) = $::DEMO_FILE =~ m#([^/]+).pl$#;
$::MW->WidgetDemo(-name => $name, -text => $::DEMO_DESCRIPTION{$name});
}
}
@MainWindow::ISA = 'WidgetWrap';
$MW->title('Perl/Tk Widget Demonstration');
$MW->iconname('widget');
$FONT = 'Helvetica 12';
my $widget_lib = Tcl::pTk->findINC('demos/widget_lib');
my $wd = "$widget_lib/WidgetDemo.pm";
$WIDTRIB = Tcl::pTk->findINC('demos/widtrib');
unless (1) {
$WIDTRIB = $ENV{WIDTRIB} if defined $ENV{WIDTRIB};
$WIDTRIB = $ARGV[0] if defined $ARGV[0];
}
## Create images used for the buttons:
my $imageView = $MW->Photo('imageView', -format => 'GIF',
-data => ' R0lGODlhEAAQAKIHAP///wwMDAAAAMDAwNnZ2SYmJmZmZv///yH5BAEAAAcALAAA
AAAQABAAAANMKLos90+ASamDRxJCgw9YVnlDOXiQBgRDBRgHKE6sW8QR3doPKK27
yg33q/GIOhdg6OsEJzeZykiBSUcs06e56Xx6np8ScIkFGuhQAgA7'
);
my $imageDelete = $MW->Photo('imageDelete', -format => 'GIF',
-data => ' R0lGODlhEAAQAIABAIQAAP///yH5BAEAAAEALAAAAAAQABAAAAIjjI+pmwAc3HGy
PUSvqYpuvWQg40FfSVacBa5nN6JYDI3mzRQAOw=='
);
my $imageRefresh = $MW->Photo('imageRefresh', -format => 'GIF',
-data => ' R0lGODlhEAAQAJEDAP///wAAACpnKv///yH5BAEAAAMALAAAAAAQABAAAAI63IKp
xgcPH2ouwgBCw1HIxHCQ4F3hSJKmwZXqWrmWxj7lKJ2dndcon9EBUq+gz3brVXAR
2tICU0gXBQA7'
);
# Try using darkmode-friendly PNG "NEW", otherwise fallback to GIF "NEW"
my $imageNew;
eval {
$imageNew = $MW->Photo('imageNew', -format => 'PNG', -data => q{
iVBORw0KGgoAAAANSUhEUgAAAB4AAAAOCAYAAAA45qw5AAACMElEQVR4AeVTAwxd
QRCc2tZHGtQ2w9q2bdsOa9u2bUW1bdt2Z372JZe6DapJLqtb3h7+T8yKi5j4CsYD
EUQXxETclT7kWOlH2VV+tFkdQHPSwksSISF+BauCqL0qgOcMWgGfgEkaMsHxqUBk
3plE/sOnh/qDPAPJH/CKFBivGHWzFwBRnHhlqbu1Mh6CoFNnC/JshQ9p4YC2lrKt
DCAV+THiVejyhMjAbrNSrroiEfKR9g7ZfCgOog8QfnUQV62wAk68ndQ9ZbyoWO1H
Y6eDY1LCQL6a9ApOp9Hi1T0+gQq2JKMlky/oTKQliKWxEZvyG575kpW4pl1aZnQK
CLOVt45Lkp8uXp2SL8KO6uitNTZLdpK6s+I/eZbhpmsmWeOGOVQNKYLITzpKPAO3
tY7LSNZ7ccSLxX9y3uuOxRkg3dKESMoCHvL+GRVCutXsB3guLgDCeXOv4iWWkvwG
BaS+PmlpK6SI9ApI2oC2UtrwZQEkhkH+NtolVlQXJl1I+QltuU3XEc721bIRFpa8
IA5iqTo6vNNWmkNBLQbPeXwF2g17Q94nTQAfY3YzeY+WSu8MDzQ2kpELUhSGJUHE
0zeR3rY1L+Xl5G/re+jbiK6KhThwwInsts1fbMUUcpZszKeVtggZEiGdZDe5AtHh
7vL4CGiRvvKPS8FAvq9Nr4ZkFadR2y6kggu1z4vlyIbBp6BugQ8JLEg4bTkD9eMZ
QZ8hpJ3VvTtuvbWrY/ElvP/9R+Aj3603+iE3fkEAAAAASUVORK5CYII=},
);
1;
} or do {
$imageNew = $MW->Photo('imageNew', -format => 'GIF', -data => q{
R0lGODlhHgAOALMPALMAANyIiOu7u8dEROaqqvru7sxVVeGZmbgREfXd3b0iItZ3
d8IzM9FmZvDMzP///yH5BAEAAA8ALAAAAAAeAA4AAASa8MlJq7046827WVOCHEkw
nANhUgJlEBIABJIwL3K+4IcUALCHjfbItYZDSgJgkBiYPmBMAUAkkLPKs/BAyLgM
wAQwOAAY2ByCaw4QAFQSoDEePJ6DmU1xInYZTw5nOEFFdgVUelkVDTIMd3AKFGQ1
MgI2AwEmQW8APZ0gdRONAks5nhIFVVxdAAkUAS2pAVwFl7ITB4UqHb0XEQA7},
);
};
# The code below creates the main window, consisting of a menu bar
# and a text widget that explains how to use the program, plus lists
# all of the demos as hypertext items.
my $file = $menubar->cascade(qw/-label File -underline 0 -menuitems/ =>
[
[cascade => '~View', -menuitems =>
[
[command => '~widget', -command => [\&view_widget, __FILE__]],
[command => '~WidgetDemo', -command => [\&view_widget, $wd]],
], # end cascade menuitems
], # end view cascade
'',
[command => '~Quit', -command => sub{ $MW->destroy }],
]);
my $help = $menubar->cascade(qw/-label Help -underline 0 -menuitems/ =>
[
[command => '~About'],
]);
my $T = $MW->Scrolled('ROText',
-scrollbars => 'e',
-wrap => 'word',
-width => 70,
-height => 30,
-font => $FONT,
-setgrid => 1,
-highlightthickness => 0,
-padx => 4,
-pady => 2,
-takefocus => 0,
)->grid(qw/-sticky nsew/);
$MW->gridRowconfigure( 0, -weight => 1); # allow expansion in both ...
$MW->gridColumnconfigure(0, -weight => 1); # ... X and Y dimensions
my $STATUS_VAR;
my $status = $MW->Label(-textvariable => \$STATUS_VAR, qw/-anchor w/);
$status->grid(qw/-sticky ew/);
# Create a bunch of tags to use in the text widget, such as those for
# section titles and demo descriptions. Also define the bindings for
# tags.
$T->tagConfigure(qw/title -font -*-Helvetica-Bold-R-Normal--*-180-*-*-*-*-*-*/);
$T->tagConfigure(qw/demo -lmargin1 1c -lmargin2 1c -foreground blue/);
if ($MW->depth == 1) {
$T->tagConfigure(qw/hot -background black -foreground white/);
$T->tagConfigure(qw/visited -lmargin1 1c -lmargin2 1c -underline 1/);
} else {
$T->tagConfigure(qw/hot -relief raised -borderwidth 1 -foreground red/);
$T->tagConfigure(qw/visited -lmargin1 1c -lmargin2 1c -foreground/ =>
'#303080');
if ($MW->windowingsystem eq 'aqua' and $MW->interp->Eval(
'package vcompare $tk_patchLevel 8.6.11') != -1) {
$T->tagConfigure('demo', -foreground => 'systemLinkColor');
$T->tagConfigure('visited', -foreground => 'purple');
}
}
$T->tagConfigure('bold', -font => 'Helvetica 12 bold');
$T->tagBind(qw/demo <ButtonRelease-1>/ => \&invoke);
my $last_line = '';
$T->tagBind(qw/demo <Enter>/ => [sub {
my($text, $sv, $x, $y) = @_;
$last_line = $text->index("\@$x,$y linestart");
$text->tagAdd('hot', $last_line, "$last_line lineend");
$text->configure(qw/-cursor hand2/);
show_stat $sv, $text, $text->index('current');
}, \$STATUS_VAR, Ev('x'), Ev('y')]
);
$T->tagBind(qw/demo <Leave>/ => [sub {
my($text, $sv) = @_;
$text->tagRemove(qw/hot 1.0 end/);
$text->configure(qw/-cursor xterm/);
$$sv = '';
}, \$STATUS_VAR]
);
$T->tagBind(qw/demo <Motion>/ => [sub {
my($text, $sv, $x, $y) = @_;
my $new_line = $text->index("\@$x,$y linestart");
if ($new_line ne $last_line) {
$text->tagRemove(qw/hot 1.0 end/);
$last_line = $new_line;
$text->tagAdd('hot', $last_line, "$last_line lineend");
}
show_stat $sv, $text, $text->index('current');
}, \$STATUS_VAR, Ev('x'), Ev('y')]
);
# Create the text for the text widget.
$T->insert('end', "Perl/Tk Widget Demonstrations\n", 'title');
$T->insert('end',
"\nThis application provides a front end for several short scripts that demonstrate what you can do with Tk widgets. Each of the numbered lines below describes a demonstration; you can click on it to invoke the demonstration. Once the demonstration window appears, you can click the ", '', 'See Code', 'bold', " button to see the Perl/Tk code that created the demonstration. If you wish, you can edit the code and click the ", '', "Rerun Demo", 'bold', " button in the code window to reinvoke the demonstration with the modified code.\n"
);
demos 'Getting Started', (
'hello' => 'An introduction to Perl/Tk',
);
demos 'Labels, buttons, checkbuttons, and radiobuttons', (
'labels' => 'Labels (text and images)',
'unicodeout' => 'Labels and Unicode text',
'button' => 'Buttons',
'check' => 'Checkbuttons (select any of a group)',
'radio' => 'Radiobuttons (select one of a group)',
'puzzle' => 'A 15-puzzle game made out of buttons',
'icon' => 'Iconic buttons that use bitmaps',
'image1' => 'Two labels displaying images',
'image2' => 'A simple user interface for viewing images',
'labelframe' => 'Labelled frames',
'ttkbut' => '*The simple Themed Tk widgets',
);
demos 'Listboxes and Trees', (
'states' => 'The 50 states',
'colors' => 'Change widget\'s color scheme',
'sayings' => 'A collection of famous and infamous sayings',
'mclist' => '*A multi-column list of countries',
);
demos 'Entries, Spin-boxes and Combo-boxes', (
'entry1' => 'Entries without scrollbars',
'entry2' => 'Entries with scrollbars',
'entry3' => 'Validated entries and password fields',
'spin' => 'Spin-boxes',
'combo' => '*Combo-boxes',
'form' => 'Simple Rolodex-like form',
);
demos 'Text', (
'texts' => 'Basic editable text',
'style' => 'Text display styles',
'bind' => 'Hypertext (tag bindings)',
'twind' => 'A text widget with embedded windows',
'search' => 'A search tool built with a text widget',
);
demos 'Canvases', (
'items' => 'The canvas item types',
'plot' => 'A simple 2-D plot',
'ctext' => 'Text items in canvases',
'arrows' => 'An editor for arrowheads on canvas lines',
'ruler' => 'A ruler with adjustable tab stops',
'floor' => 'A building floor plan',
'cscroll' => 'A simple scrollable canvas',
'transtile' => 'Tiles and transparent images',
);
demos 'Scales and Progress Bars', (
'hscale' => 'Horizontal scale',
'vscale' => 'Vertical scale',
'ttkprogressbar' => '*Progress bar',
);
demos 'Paned Windows and Notebooks', (
'paned1' => 'Horizontal paned window',
'paned2' => 'Vertical paned window',
'ttkpane' => '*Themed nested panes',
'ttknote' => '*Themed notebook',
);
demos 'Photos and Images', (
'photo1' => 'Transparent pixels',
'photo2' => 'Alpha channel compositing',
);
demos 'Menus and Toolbars', (
'menus' => 'Menus and cascades (sub-menus)',
'menus2' => 'As above, but using Perl/Tk -menuitems',
'menbut' => 'Menubuttons',
'ttkmenu' => '*Themed menu buttons',
'toolbar' => '*Themed toolbar',
);
demos 'Common Dialogs', (
'msgBox' => 'Message boxes',
'filebox' => 'File selection dialog',
'choosedir' => 'Directory selection dialog',
'clrpick' => 'Color picker',
);
demos 'Tix Widgets', (
'balloon' => 'Popup help window when mouse lingers over widget',
'browseentry' => 'Entry with Listbox to select list values',
'browseentry2' => 'Another BrowseEntry example',
'dirtree' => 'Display a directory tree',
'form_mgr' => 'The form geometry manager',
'HList' => 'A hierarchical listbox widget',
'HList2' => 'Multicolumn listbox with individual cell styles',
'labframe' => 'A Frame with embedded label',
);
demos 'Simulations', (
'bounce' => 'Balls bouncing in a cavity',
);
demos 'Sample Perl Mega-Widgets', (
'mega' => 'Introduction to writing pure Perl mega-widgets',
'slide' => 'Composite Tk::SlideSwitch - binary on/off switch',
'trace2' => 'Derived Tk::TraceText - Text contents defined by a traced variable',
);
demos 'Miscellaneous', (
'bitmaps' => 'The built-in bitmaps',
'dialog1' => 'A dialog box with a local grab',
'dialog2' => 'A dialog box with a global grab',
'trace1' => 'Trace a variable\'s value',
'progress' => 'Various ProgressBar widgets',
'keysyms' => 'Show keyboard symbols',
'virtevents1' => 'Synthesizing events',
);
$T->insert('end', "\n", '', "User Contributed Demonstrations\n", 'title');
opendir(C, $WIDTRIB) or warn "Cannot open $WIDTRIB: $!";
my(@dirent) = grep /^.+\.pl$/, sort(readdir C);
closedir C;
unshift @dirent, 'TEMPLATE.pl'; # I want it first
my $i = 0;
while ($_ = shift @dirent) {
next if /TEMPLATE\.pl/ and $i != 0;
unless (open(C, "$WIDTRIB/$_")) {
warn "Cannot open $_: $!" unless /TEMPLATE\.pl/;
next;
}
my($name) = /^(.*)\.pl$/;
$_ = <C>;
my($title) = /^#\s*(.*)$/;
$DEMO_DESCRIPTION{$name} = $title;
close C;
$T->insert('end', ++$i . ". $title\n", ['demo', "demo-$name"]);
}
build_about_dialog $help;
$T->focus;
MainLoop;
sub AUTOLOAD {
# This routine handles the loading of most demo methods.
my($demo) = @_;
$T->Busy;
{
$DEMO_FILE = "$WIDTRIB/${demo}.pl" if -f "$WIDTRIB/${demo}.pl";
$DEMO_FILE = "$widget_lib/${demo}.pl" if -f "$widget_lib/${demo}.pl";
do $DEMO_FILE;
warn $@ if $@;
}
$T->Unbusy;
goto &$::AUTOLOAD if defined &$::AUTOLOAD;
} # end AUTOLOAD
sub build_about_dialog {
my $help = shift;
my $dialog = $MW->DialogBox(
-title => "Perl/Tk, based on Tcl/Tk ${Tcl::pTk::VERSION}",
-default_button => 'OK',
-buttons => ['OK'],
);
my $df = $dialog->add( 'Labelframe', -text => ' 2004 - Feb - 11 ' );
$df->pack( qw/ -fill both -expand 1 / );
my( $tk_date ) = scalar localtime( ( stat $Tcl::pTk::library )[9] );
my( $tk ) = "$Tcl::pTk::VERSION, $tk_date";
foreach my $item (
[ 'Perl', "$], $Config{cf_time}" ],
[ 'Tcl::pTk', $tk ],
[ 'Platform', $Tcl::pTk::platform ],
[ 'Library', $Tcl::pTk::library ],
) {
my $l = $item->[0] . ':';
my $le = $df->LabEntry(
-font => $FONT,
-label => ' ' x (13 - length $l) . $l,
-labelPack => [qw/-side left -anchor w/],
-labelFont => 'Courier 12 bold',
-relief => 'flat',
-takefocus => 0,
-textvariable => \$item->[1],
-width => 45,
);
$le->pack(qw/ -fill x -expand 1/);
$le->Subwidget( 'entry' )->bindtags( [ ] );
}
$help->cget(-menu)->entryconfigure('About',
-command => [$dialog => 'Show'],
);
} # end build_about_dialog
sub demos {
# Generate one demo section.
my ($title, @demos) = @_;
$T->insert('end', "\n", '', "$title\n", 'title');
for (my $n = 0; $n <= $#demos; $n += 2) {
my $desc = $demos[$n+1];
my $script = $demos[$n];
my $new = 0;
$new = 1 if( $desc =~ s/^\*// ); # Add "New" if desc starts with a '*'
$T->insert('end', $n / 2 + 1 . '. ' . $desc,
['demo', 'demo-' . $script]);
if( $new ){
$T->imageCreate('end', -image => 'imageNew', -padx => 5);
}
$T->insert('end', " \n");
}
} # end demos
sub invoke {
# This procedure is called when the user clicks on a demo description.
my($text) = @_;
my $index = $text->index('current');
my @tags = $T->tagNames($index);
my $i = lsearch('demo\-.*', @tags);
return if $i < 0;
my($demo) = $tags[$i] =~ /demo-(.*)/;
$T->tagAdd('visited', "$index linestart", "$index lineend");
{
no strict 'refs';
$SIG{__WARN__} = sub {
print @_ unless $_[0] =~ /Subroutine \S+ redefined at/;
};
&$demo($demo);
}
} # end invoke
sub lsearch {
# Search the list using the supplied regular expression and return its
# ordinal, or -1 if not found.
my($regexp, @list) = @_;
my($i);
for ($i=0; $i<=$#list; $i++) {
return $i if $list[$i] =~ /$regexp/;
}
return -1;
} # end lsearch
sub see_code {
# This procedure creates a toplevel window that displays the code for
# a demonstration and allows it to be edited and reinvoked.
my($demo) = @_;
my $file;
$file = ( $demo =~ /\.pl$/ ) ? $demo : "${demo}.pl";
if (not Exists $CODE) {
$CODE = $MW->Toplevel;
my $code_buttons = $CODE->Frame;
# Use Tile Buttons if available
my $buttonWidget = $Tcl::pTk::_Tile_available ? 'ttkButton' : 'Button';
$code_buttons->pack(qw/-side bottom -fill x/);
my $code_buttons_dismiss = $code_buttons->$buttonWidget(
-text => 'Dismiss',
-image => 'imageDelete',
-compound => 'left',
-command => [$CODE => 'withdraw'],
);
$CODE_RERUN = $code_buttons->$buttonWidget(-text => 'Rerun Demo', -compound => 'left', -image => 'imageRefresh');
$CODE_TEXT = $CODE->Scrolled('Text', qw/ -height 40 -scrollbars oe /);
$code_buttons_dismiss->pack(qw/-side left -expand 1/);
$CODE_RERUN->pack(qw/-side left -expand 1/);
$CODE_TEXT->pack(qw/-side left -expand 1 -fill both/);
} else {
$CODE->deiconify;
$CODE->raise;
}
$CODE_RERUN->configure(-command => sub {
eval $CODE_TEXT->get(qw/1.0 end/);
{
no strict 'refs';
&$demo($demo);
}
});
$CODE->iconname($file);
$file = "$WIDTRIB/$file" if -f "$WIDTRIB/$file";
$file = "$widget_lib/$file" if -f "$widget_lib/$file";
$CODE->title("Demo code: $file");
$CODE_TEXT->delete(qw/1.0 end/);
open(CODE, "<$file") or warn "Cannot open demo file $file: $!";
{
local $/ = undef;
$CODE_TEXT->insert('1.0', <CODE>);
}
close CODE;
$CODE_TEXT->markSet(qw/insert 1.0/);
$CODE_TEXT->focus;
} # end see_code
sub see_vars {
# Create a top-level window that displays a bunch of global variable values
# and keeps the display up-to-date even when the variables change value.
# $args is a pointer to a list of list of 2:
#
# ["variable description", \$VAR]
#
# The old trick of passing a string to serve as the description and a soft
# reference to the variable no longer works with lexicals and use strict.
my($parent, $args) = @_;
$VARS->destroy if Exists($VARS);
$VARS = $parent->Toplevel;
$VARS->geometry('+300+300');
$VARS->title('Variable Values');
$VARS->iconname('Variables');
my $title = $VARS->Label(
-text => 'Variable Values:',
-width => 20,
-anchor => 'center',
-font => '-*-helvetica-medium-r-normal--*-180-*-*-*-*-*-*',
);
$title->pack(qw/-side top -fill x/);
my($label, $var);
foreach my $i (@$args) {
($label, $var) = @$i;
my $wf = $VARS->Frame->pack(qw/-anchor w/);
$wf->Label(-text => "$label: ")->pack(qw/-side left/);
$wf->Label(-textvariable => $var)->pack(qw/-side left/);
}
$VARS->Button(-text => 'OK', -command => [$VARS => 'destroy'])->
pack(qw/-side bottom -pady 2/);
} # end see_vars
sub show_stat {
# Display name of current demonstration. $sv is a reference to the
# status Label -textvariable, $text is the Text widget reference and
# $index is the demonstration index in the Text widget.
my($sv, $text, $index) = @_;
my @tags = $text->tagNames($index);
my $i = lsearch('demo\-.*', @tags);
return if $i < 0;
my($demo) = $tags[$i] =~ /demo-(.*)/;
$$sv = "Click Button-1 to run the \"$demo\" demonstration.";
} # end show_stat
sub view_widget {
# Expose a file's innards to the world too, but only for viewing.
my($widget) = @_;
if (not Exists $VIEW) {
$VIEW = $MW->Toplevel;
$VIEW->iconname('widget');
my $view_buttons = $VIEW->Frame;
$view_buttons->pack(qw/-side bottom -fill x/);
my $view_buttons_dismiss = $view_buttons->Button(
-text => 'Dismiss',
-command => [$VIEW => 'withdraw'],
);
$view_buttons_dismiss->pack(qw/-side left -expand 1/);
$VIEW_TEXT = $VIEW->Scrolled('Text', qw/ -height 40 -scrollbars oe /);
$VIEW_TEXT->pack(qw/-side left -expand 1 -fill both/);
} else {
$VIEW->deiconify;
$VIEW->raise;
}
$VIEW->title("Demo code: $widget");
$VIEW_TEXT->configure(qw/-state normal/);
$VIEW_TEXT->delete(qw/1.0 end/);
open(VIEW, "<$widget") or warn "Cannot open demo file $widget: $!";
{
local $/ = undef;
$VIEW_TEXT->insert('1.0', <VIEW>);
}
close VIEW;
$VIEW_TEXT->markSet(qw/insert 1.0/);
$VIEW_TEXT->configure(qw/-state disabled/);
} # end view_widget
=head1 NAME
widget - Demonstration of Perl/Tk widgets
=head1 SYNOPSYS
widget [ directory ]
=head1 DESCRIPTION
This script demonstrates the various widgets provided by Tk, along with
many of the features of the Tk toolkit. This file only contains code to
generate the main window for the application, which invokes individual
demonstrations. The code for the actual demonstrations is contained in
separate ".pl" files in the "widget_lib" directory, which are autoloaded
by this script as needed.
widget looks in the directory specified on the command line to load user
contributed demonstrations. If no directory name is specified when widget is
invoked and the environment variable WIDTRIB is defined then demonstrations
are loaded from the WIDTRIB directory. If WIDTRIB is undefined then widget
defaults to the released user contributed directory, "widtrib".
=head2 History
#
# Stephen O. Lidie, LUCC, 96/03/11. [email protected]
# Stephen O. Lidie, LUCC, 97/01/01. [email protected]
# Stephen O. Lidie, LUCC, 97/02/11. [email protected]
# Stephen O. Lidie, LUCC, 97/06/07. [email protected]
# Update for Tk402.00x. Total revamp: WidgetDemo, Scrolled, released
# composites, -menuitems, qw//, etcetera. Perl 5.004 required.
# Stephen O. Lidie, LUCC, 98/03/10. [email protected]
# Update for Tk8.
# Stephen O. Lidie, LUCC, 98/06/26. [email protected]
# Add Common Dialogs for Tk800.007.
# [email protected], 1999/11/29, Lehigh University.
# Demo some "dash patch" changes.
# [email protected], 2000/01/11, Lehigh University.
# Update menubar to Tk 8, fix color palette Menubutton demo.
# [email protected], 2000/07/06, Lehigh University.
# Remove inswt() from widget and styles.pl to show the proper Perl/Tk
# idiom for inserting Text tags. Various and sundry cleanups.
# [email protected], 2003/07/29, Lehigh University Computing Center.
# Update for Tk 8.4.4.
=head1 AUTHOR
Steve Lidie <[email protected]>
=cut