File: text_demo

package info (click to toggle)
perl-tk 1:800.025-2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 18,444 kB
  • ctags: 19,081
  • sloc: ansic: 206,740; perl: 40,187; makefile: 4,371; sh: 2,373; yacc: 762
file content (96 lines) | stat: -rwxr-xr-x 3,337 bytes parent folder | download | duplicates (5)
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
#!/usr/local/bin/perl -w

use Tk;
require Tk::Text;

my $top = MainWindow->new;
$top->option('add','*Text.background'=>'white');

my $t = $top->Scrolled('Text',"-relief" => "raised",
                     "-bd" => "2",
                     "-setgrid" => "true");

my $m = $t->Menu();
$m->add("command", "-label" => "Open", "-underline" => 0, 
        "-command" => \&sayopen);
$m->add("command", "-label" => "Close", "-underline" => 0, 
        "-command" => \&sayclose);
$m->add("separator");
$m->add("command", "-label" => "Selection", "-underline" => 0, 
        "-command" => \&showsel);
$m->add("separator");
$m->add("command", "-label" => "Exit", "-underline" => 1, 
        "-command" => \&doexit);

$t->pack(-expand => 1, "-fill"   => "both");

$t->tag("configure", "underline","-underline","on");

$t->insert("0.0", "This window is a text widget.  It displays one or more lines of text
and allows you to edit the text.  Here is a summary of the things you
can do to a text widget:

1. Insert text. Press mouse button 1 to set the insertion cursor, then
type text.  What you type will be added to the widget.  You can backspace
over what you've typed using either the backspace key, the delete key,
or Control+h.

2. Resize the window.  This widget has been configured with the \"setGrid\"
option on, so that if you resize the window it will always resize to an
even number of characters high and wide.  Also, if you make the window
narrow you can see that long lines automatically wrap around onto
additional lines so that all the information is always visible.

3. Scanning. Press mouse button 2 in the text window and drag up or down.
This will drag the text at high speed to allow you to scan its contents.

4. Select. Press mouse button 1 and drag to select a range of characters.
Once you've released the button, you can adjust the selection by pressing
button 1 with the shift key down.  This will reset the end of the
selection nearest the mouse cursor and you can drag that end of the
selection by dragging the mouse before releasing the mouse button.
You can double-click to select whole words, or triple-click to select
whole lines.

5. Delete. To delete text, select the characters you'd like to delete
and type Control+d.

6. Copy the selection. To copy the selection either from this window
or from any other window or application, select what you want, click
button 1 to set the insertion cursor, then type Control+v to copy the
selection to the point of the insertion cursor.

You can also bind commands to tags. Like press button 3 for menu ");

&insertwtag($t,"here","underline");

$t->tag("bind","underline","<3>", [sub { shift; shift->Post(@_)},$m,Ev(X),Ev(Y)] );

$t->bind("<Any-Enter>", sub { $t->focus });

$t->Subwidget('text')->OnDestroy(sub { print "Destroyed!\n"; print $t->get('1.0','end') });


Tk::MainLoop;

sub insertwtag {
  local($w,$text,$tag) = @_;
  my $start = $w->index("insert");
  print "start=$start\n";
  $w->insert("insert",$text);
  $w->tag("add",$tag,$start,"insert");
}


sub sayopen { print "Open something\n" }
sub sayclose { print "Close something\n" }
sub showsel  { my @info = $t->tagRanges('sel');
               if (@info)
                {
                 print "start=$info[0] end=$info[1]\n" 
                }
             }
sub doexit 
{ 
 die "'die' no longer exits"
}