summaryrefslogtreecommitdiff
path: root/Master/tlpkg/tlperl/lib/Tk/demos/widget_lib/trace2.pl
blob: 9d7c38806d524d07c390f30dcc7fd56e37996140 (plain)
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
# trace2.pl

$Tk::TraceText::VERSION = '1.0';

package Tk::TraceText;

use Tk::widgets qw/ Trace /;
use base qw/ Tk::Derived Tk::Text /;
use strict;

Construct Tk::Widget 'TraceText';

sub Populate {

    my( $self, $args ) = @_;

    $self->SUPER::Populate( $args );

    $self->ConfigSpecs(
        -textvariable => [ 'METHOD', 'textVariable', 'TextVariable', undef ],
    );

    $self->OnDestroy( sub {
	my $vref = $self->{_vref};
	$self->traceVdelete ( $vref ) if defined $vref;
    } );

} # end Populate

# Private methods.

sub insdel {

    my( $self, $sub, @args ) = @_;
    
    $self->{_busy} = 1;
    $self->$sub( @args );
    my $vref = $self->{_vref};
    $$vref = $self->get( qw/1.0 end/ );
    $self->{_busy} = 0;

} # end insedel

sub textvariable {

    my ( $self, $vref ) = @_;

    $self->traceVariable( $vref, 'w', [ \&tracew => $self, $vref ] );
    $self->{_vref} = $vref;
    
} # end textvariable

sub tracew {

    my ( $index, $value, $op, $self, $vref ) = @_;

    return unless defined $self;	# if app is being destroyed
    return if $self->{_busy};

    if ( $op eq 'w' ) {
	$self->delete( qw/1.0 end/ );
	$self->insert( '1.0', $value );
    } elsif ( $op eq 'r' ) {
    } elsif ( $op eq 'u' ) {
	$self->traceVdelete ( $vref );
    }

} # end tracew

# Overridden methods.

sub delete {

    my ( $self, @args ) = @_;

    $self->insdel( 'SUPER::delete', @args )

} # end delete

sub insert {

    my ( $self, @args ) = @_;

    $self->insdel( 'SUPER::insert', @args );

} # end insert

1;

package main;

use Tk::widgets qw/ Trace /;
use vars qw / $TOP /;
use strict;

sub trace2 {

    my( $demo ) = @_;

    $TOP = $MW->WidgetDemo(
        -name             => $demo,
        -text             => "This demonstration derives a new Text widget whose contents are modified using a normal Perl variable.",
        -title            => 'Contents of a Text widget tied to a variable',
        -iconname         => 'trace2',
    );

    my $mw = $TOP;
    my $tt = $mw->Scrolled( 'TraceText', -textvariable => \my $frog )->grid;
    $tt->focus;

    $mw->traceVariable( \$frog, 'wu', [ \&trace2_tracefrog, $mw, \$frog ] );

    $frog = "Frogs lacking lipophores are blue.";

} # end trace2

sub trace2_tracefrog {

    my( $index, $value, $op ) = @_;

    print "Final " if $op eq 'u';
    print "User trace: $value";
    return $value;

}

__END__

=head1 NAME

Tk::TraceText - Text contents defined by a traced variable.

=for pm Tk/TraceText.pm

=for category Text

=head1 SYNOPSIS

 $tt = $parent->TraceText(-option => value, ... );

=head1 DESCRIPTION

Create a new B<TraceText> widget that is derived from the standard B<Text>
widget. Because it inherits all the base options and methods it behaves
just like a B<Text> widget.  Additionally, B<TraceText> adds a -textvariable
option, which is a reference to a Perl scalar that defines the contents of
the widget.

Based on the Tcl/Tk TracedText "overridden widget" by Kevin Kenny.

=over 4

=item B<-textvariable>

A scalar reference.  The value of the variable defines the contents of the
TraceText widget.  Using the keyboard to insert or delete text changes the 
value of the variable, and changing the variable alters the contents of the
TraceText widget.

=back

=head1 METHODS

Standard Text widget methods.

=head1 ADVERTISED SUBWIDGETS

None.

=head1 EXAMPLE

 my $tt = $mw->TraceText( -textvariable => \$scalar );

=head1 AUTHOR

Stephen.O.Lidie@Lehigh.EDU

Copyright (C) 2003 - 2004, Steve Lidie. All rights reserved.

This program is free software; you can redistribute it
and/or modify it under the same terms as Perl itself.

=head1 KEYWORDS

text, trace

=cut