Debian Codegraph @

examples/text_demo

raw
1 #!/usr/local/bin/perl -w
2
3 use Tk;
4 require Tk::Text;
5
6 my $top = MainWindow->new;
7 $top->option('add','*Text.background'=>'white');
8
9 my $t = $top->Scrolled('Text',"-relief" => "raised",
10 "-bd" => "2",
11 "-setgrid" => "true");
12
13 my $m = $t->Menu();
14 $m->add("command", "-label" => "Open", "-underline" => 0,
15 "-command" => \&sayopen);
16 $m->add("command", "-label" => "Close", "-underline" => 0,
17 "-command" => \&sayclose);
18 $m->add("separator");
19 $m->add("command", "-label" => "Selection", "-underline" => 0,
20 "-command" => \&showsel);
21 $m->add("separator");
22 $m->add("command", "-label" => "Exit", "-underline" => 1,
23 "-command" => \&doexit);
24
25 $t->pack(-expand => 1, "-fill" => "both");
26
27 $t->tag("configure", "underline","-underline","on");
28 $t->tag("configure", "hideable", -elide => 0, -foreground => 'blue');
29
30 $t->insert("0.0", "This window is a text widget. It displays one or more
31 lines of text and allows you to edit the text. Here is a summary of the
32 things you can do to a text widget:",'hideable');
33
34 $t->insert(end => "
35
36 1. Insert text. Press mouse button 1 to set the insertion cursor, then
37 type text. What you type will be added to the widget. You can backspace
38 over what you've typed using either the backspace key, the delete key,
39 or Control+h.
40
41 2. Resize the window. This widget has been configured with the \"setGrid\"
42 option on, so that if you resize the window it will always resize to an
43 even number of characters high and wide. Also, if you make the window
44 narrow you can see that long lines automatically wrap around onto
45 additional lines so that all the information is always visible.
46
47 3. Scanning. Press mouse button 2 in the text window and drag up or down.
48 This will drag the text at high speed to allow you to scan its contents.
49
50 4. Select. Press mouse button 1 and drag to select a range of characters.
51 Once you've released the button, you can adjust the selection by pressing
52 button 1 with the shift key down. This will reset the end of the
53 selection nearest the mouse cursor and you can drag that end of the
54 selection by dragging the mouse before releasing the mouse button.
55 You can double-click to select whole words, or triple-click to select
56 whole lines.
57
58 5. Delete. To delete text, select the characters you'd like to delete
59 and type Control+d.
60
61 6. Copy the selection. To copy the selection either from this window
62 or from any other window or application, select what you want, click
63 button 1 to set the insertion cursor, then type Control+v to copy the
64 selection to the point of the insertion cursor.
65
66 You can also bind commands to tags. Like press button 3 for menu ");
67
68 &insertwtag($t,"here","underline");
69
70 $t->tag("bind","underline","<3>", [sub { shift; shift->Post(@_)},$m,Ev(X),Ev(Y)] );
71
72 $t->bind("<Any-Enter>", sub { $t->focus });
73
74 $t->Subwidget('text')->OnDestroy(sub { print "Destroyed!\n"; print $t->get('1.0','end') });
75
76 $t->tag("bind", "hideable","<2>", sub {
77 $t->tagConfigure(hideable => -elide => 1, -foreground => 'pink')}
78 );
79
80
81 Tk::MainLoop;
82
83 sub insertwtag {
84 local($w,$text,$tag) = @_;
85 my $start = $w->index("insert");
86 print "start=$start\n";
87 $w->insert("insert",$text);
88 $w->tag("add",$tag,$start,"insert");
89 }
90
91
92 sub sayopen { print "Open something\n" }
93 sub sayclose { print "Close something\n" }
94 sub showsel { my @info = $t->tagRanges('sel');
95 if (@info)
96 {
97 print "start=$info[0] end=$info[1]\n"
98 }
99 }
100 sub doexit
101 {
102 die "'die' no longer exits"
103 }