| 1 | package Tkx::ROText; | ||
| 2 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | |||
| 5 | 1 | ▶ | use Carp qw'croak'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 0 | ▶ | use Tkx; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 7 | use base qw(Tkx::widget Tkx::MegaConfig); | ||
| 8 | |||
| 9 | our $VERSION = '0.06'; | ||
| 10 | |||
| 11 | __PACKAGE__->_Mega('tkx_ROText'); | ||
| 12 | |||
| 13 | __PACKAGE__->_Config( | ||
| 14 | -state => ['METHOD'], | ||
| 15 | DEFAULT => ['.text'], | ||
| 16 | ); | ||
| 17 | |||
| 18 | |||
| 19 | #------------------------------------------------------------------------------- | ||
| 20 | # Method : _Populate | ||
| 21 | # Purpose : Create a new Tkx::ROText widget | ||
| 22 | # Notes : | ||
| 23 | #------------------------------------------------------------------------------- | ||
| 24 | sub _Populate { | ||
| 25 | my $class = shift; | ||
| 26 | my $widget = shift; | ||
| 27 | my $path = shift; | ||
| 28 | my %opt = (-state => 'readonly', @_); | ||
| 29 | my $state = delete $opt{-state}; # use custom handler for this option | ||
| 30 | |||
| 31 | # create the widget | ||
| 32 | my $self = $class->new($path)->_parent | ||
| 33 | ->new_frame(-name => $path, -class => 'Tkx_ROText'); | ||
| 34 | $self->_class($class); | ||
| 35 | |||
| 36 | my $text = $self->new_text(-name => 'text', %opt); | ||
| 37 | $text->g_pack(-fill => 'both', -expand => 1); | ||
| 38 | |||
| 39 | # Rename the widget to make it private. This enables us to stub the | ||
| 40 | # insert/delete methods and make it read-only. Calling _readonly() sets up | ||
| 41 | # the handlers for public/private aliasing so that calls to the configure() | ||
| 42 | # method work. | ||
| 43 | Tkx::rename($text, $text . '.priv'); | ||
| 44 | $self->_readonly(); | ||
| 45 | |||
| 46 | $self->configure(-state => $state); | ||
| 47 | |||
| 48 | return $self; | ||
| 49 | } | ||
| 50 | |||
| 51 | |||
| 52 | #------------------------------------------------------------------------------- | ||
| 53 | # Method : _mpath | ||
| 54 | # Purpose : Delegate all method calls to the text subwidget. | ||
| 55 | # Notes : | ||
| 56 | #------------------------------------------------------------------------------- | ||
| 57 | sub _mpath { $_[0] . '.text' } | ||
| 58 | |||
| 59 | |||
| 60 | #------------------------------------------------------------------------------- | ||
| 61 | # Method : insert/delete | ||
| 62 | # Purpose : Provide methods for programmatic insertions and deletions | ||
| 63 | # Notes : The 'm_' prefix is to support method delegation from megawidgets | ||
| 64 | # that embed this one. | ||
| 65 | #------------------------------------------------------------------------------- | ||
| 66 | sub m_insert { my $self = shift; Tkx::i::call($self . '.text.priv', 'insert', @_) } | ||
| 67 | sub m_delete { my $self = shift; Tkx::i::call($self . '.text.priv', 'delete', @_) } | ||
| 68 | |||
| 69 | |||
| 70 | #------------------------------------------------------------------------------- | ||
| 71 | # Method : _config_state | ||
| 72 | # Purpose : Handler for configure(-state => <value>) | ||
| 73 | # Notes : | ||
| 74 | #------------------------------------------------------------------------------- | ||
| 75 | sub _config_state { | ||
| 76 | my $self = shift; | ||
| 77 | my $state = shift; | ||
| 78 | my $path = $self . '.text'; | ||
| 79 | |||
| 80 | if (defined $state) { | ||
| 81 | |||
| 82 | if ($state eq 'readonly') { | ||
| 83 | $self->_readonly(1); | ||
| 84 | Tkx::i::call($path , 'configure', '-state', 'normal'); | ||
| 85 | } | ||
| 86 | elsif ($state eq 'normal') { | ||
| 87 | $self->_readonly(0); | ||
| 88 | Tkx::i::call($path, 'configure', '-state', 'normal'); | ||
| 89 | } | ||
| 90 | elsif ($state eq 'disabled') { | ||
| 91 | Tkx::i::call($path, 'configure', '-state', 'disabled'); | ||
| 92 | # The readonly state doesn't matter when the widget is disabled. | ||
| 93 | } | ||
| 94 | else { | ||
| 95 | croak qq'bad state value "$state": must be normal, disabled, or readonly'; | ||
| 96 | } | ||
| 97 | |||
| 98 | $self->_data->{-state} = $state; | ||
| 99 | } | ||
| 100 | |||
| 101 | return $self->_data->{-state}; | ||
| 102 | } | ||
| 103 | |||
| 104 | |||
| 105 | #------------------------------------------------------------------------------- | ||
| 106 | # Method : _readonly | ||
| 107 | # Purpose : Control whether widget is read-only or read/write. | ||
| 108 | # Notes : | ||
| 109 | #------------------------------------------------------------------------------- | ||
| 110 | sub _readonly { | ||
| 111 | my $self = shift; | ||
| 112 | my $ro = shift; | ||
| 113 | my $path = $self . '.text'; | ||
| 114 | |||
| 115 | if ($ro) { | ||
| 116 | |||
| 117 | Tkx::eval(<<EOT); | ||
| 118 | proc $path {args} [string map [list WIDGET $path] { | ||
| 119 | switch [lindex \$args 0] { | ||
| 120 | "insert" {} | ||
| 121 | "delete" {} | ||
| 122 | "default" { return [eval WIDGET.priv \$args] } | ||
| 123 | } | ||
| 124 | }] | ||
| 125 | EOT | ||
| 126 | |||
| 127 | } | ||
| 128 | else { | ||
| 129 | |||
| 130 | Tkx::eval(<<EOT); | ||
| 131 | proc $path {args} [string map [list WIDGET $path] { | ||
| 132 | return [eval WIDGET.priv \$args] | ||
| 133 | }] | ||
| 134 | EOT | ||
| 135 | |||
| 136 | } | ||
| 137 | } | ||
| 138 | |||
| 139 | |||
| 140 | 1; | ||
| 141 | |||
| 142 | __END__ | ||