| 1 | package Tk::CodeText::TagsEditor; | ||
| 2 | |||
| 3 | package #hide from MetaCPAN | ||
| 4 | MyColorEntry; | ||
| 5 | |||
| 6 | use strict; | ||
| 7 | use warnings; | ||
| 8 | |||
| 9 | use base qw(Tk::Derived Tk::ColorEntry); | ||
| 10 | |||
| 11 | Construct Tk::Widget 'MyColorEntry'; | ||
| 12 | |||
| 13 | sub OnEscape { | ||
| 14 | my $self = shift; | ||
| 15 | $self->SUPER::OnEscape; | ||
| 16 | $self->Callback('-command', $self->get); | ||
| 17 | } | ||
| 18 | |||
| 19 | sub OnKey { | ||
| 20 | my $self = shift; | ||
| 21 | $self->SUPER::OnKey; | ||
| 22 | my $val = $self->Subwidget('Entry')->get; | ||
| 23 | $self->Callback('-command', $val) if $self->validate($val); | ||
| 24 | $self->Callback('-command', '') if $self->Subwidget('Entry')->get eq ''; | ||
| 25 | } | ||
| 26 | |||
| 27 | sub popDown { | ||
| 28 | my $self = shift; | ||
| 29 | $self->SUPER::popDown; | ||
| 30 | $self->Callback('-command', '') if $self->get eq ''; | ||
| 31 | } | ||
| 32 | |||
| 33 | ########################################################################### | ||
| 34 | |||
| 35 | package Tk::CodeText::TagsEditor; | ||
| 36 | |||
| 37 | =head1 NAME | ||
| 38 | |||
| 39 | Tk:CodeText::TagsEditor - Edit highlighting tags for L<Tk::CodeText> | ||
| 40 | |||
| 41 | =cut | ||
| 42 | |||
| 43 | use strict; | ||
| 44 | use warnings; | ||
| 45 | use vars qw($VERSION); | ||
| 46 | $VERSION = '0.52'; | ||
| 47 | |||
| 48 | use base qw(Tk::Derived Tk::Frame); | ||
| 49 | |||
| 50 | require Tk::ColorEntry; | ||
| 51 | require Tk::PopColor; | ||
| 52 | require Tk::HList; | ||
| 53 | require Tk::Balloon; | ||
| 54 | use Tk::CodeText::Theme; | ||
| 55 | |||
| 56 | Construct Tk::Widget 'TagsEditor'; | ||
| 57 | |||
| 58 | =head1 SYNOPSIS | ||
| 59 | |||
| 60 | require Tk::CodeText::TagsEditor; | ||
| 61 | my $text= $window->TagsEditor(@options)->pack; | ||
| 62 | |||
| 63 | =head1 DESCRIPTION | ||
| 64 | |||
| 65 | Inherits L<Tk::Frame>. | ||
| 66 | |||
| 67 | Provides an editor module for the color and font tags used for | ||
| 68 | syntax highlighting in L<Tk::CodeText>. | ||
| 69 | |||
| 70 | =head1 OPTIONS | ||
| 71 | |||
| 72 | =over 4 | ||
| 73 | |||
| 74 | =item Switch: B<-balloon> | ||
| 75 | |||
| 76 | If you already have it available you can supply a reference to a | ||
| 77 | balloon widget. If you do not, it will create it's own balloon | ||
| 78 | widget. | ||
| 79 | |||
| 80 | Only available at create time. | ||
| 81 | |||
| 82 | =item Switch: B<-defaultbackground> | ||
| 83 | |||
| 84 | Mandatory! | ||
| 85 | |||
| 86 | Give it the background color of your text widget as value. | ||
| 87 | |||
| 88 | =item Switch: B<-defaultforeground> | ||
| 89 | |||
| 90 | Mandatory! | ||
| 91 | |||
| 92 | Give it the foreground color of your text widget as value. | ||
| 93 | |||
| 94 | =item Switch: B<-defaultfont> | ||
| 95 | |||
| 96 | Mandatory! | ||
| 97 | |||
| 98 | Give it the font object of your text widget as value. | ||
| 99 | |||
| 100 | =item Switch: B<-historyfile> | ||
| 101 | |||
| 102 | The name of the file where all the L<Tk::ColorEntry> widgets | ||
| 103 | store their recently used colors. If you do not specify this, | ||
| 104 | the history of the ColorEntry widgets will not propagate between | ||
| 105 | them. | ||
| 106 | |||
| 107 | Only available at create time. | ||
| 108 | |||
| 109 | =item Switch: B<-theme> | ||
| 110 | |||
| 111 | You can supply a reference to a L<Tk::CodeText::Theme> object. | ||
| 112 | If you do not, it will create it's own. | ||
| 113 | |||
| 114 | Only available at create time. | ||
| 115 | |||
| 116 | =back | ||
| 117 | |||
| 118 | =head1 METHODS | ||
| 119 | |||
| 120 | =over 4 | ||
| 121 | |||
| 122 | =cut | ||
| 123 | |||
| 124 | sub Populate { | ||
| 125 | my ($self,$args) = @_; | ||
| 126 | |||
| 127 | my $historyfile = delete $args->{'-historyfile'}; | ||
| 128 | |||
| 129 | my $balloon = delete $args->{'-balloon'}; | ||
| 130 | $balloon = $self->Balloon unless defined $balloon; | ||
| 131 | |||
| 132 | my $theme = delete $args->{'-theme'}; | ||
| 133 | $theme = Tk::CodeText::Theme->new unless defined $theme; | ||
| 134 | |||
| 135 | my $defaultbackground = delete $args->{'-defaultbackground'}; | ||
| 136 | die 'You must specify the -defaultbackground option' unless defined $defaultbackground; | ||
| 137 | |||
| 138 | my $defaultforeground = delete $args->{'-defaultforeground'}; | ||
| 139 | die 'You must specify the -defaultforeground option' unless defined $defaultforeground; | ||
| 140 | |||
| 141 | my $defaultfont = delete $args->{'-defaultfont'}; | ||
| 142 | die 'You must specify the -defaultfont option' unless defined $defaultfont; | ||
| 143 | |||
| 144 | $self->SUPER::Populate($args); | ||
| 145 | |||
| 146 | $self->{THEME} = $theme; | ||
| 147 | |||
| 148 | my @columns = ('Tag', 'Foreground', 'Background', 'Bold', 'Italic'); | ||
| 149 | my $list; | ||
| 150 | |||
| 151 | #hack changing list background | ||
| 152 | my $l = $self->Label; | ||
| 153 | my $bg = $l->cget('-background'); | ||
| 154 | $l->destroy; | ||
| 155 | |||
| 156 | $list = $self->Scrolled('HList', | ||
| 157 | -background => $bg, | ||
| 158 | -browsecmd => sub { $list->selectionClear; $list->anchorClear }, | ||
| 159 | -columns => 5, | ||
| 160 | -header => 1, | ||
| 161 | -scrollbars => 'osoe', | ||
| 162 | )->pack(-expand => 1, -fill => 'both'); | ||
| 163 | $self->{HLIST} = $list; | ||
| 164 | my $count = 0; | ||
| 165 | for (@columns) { | ||
| 166 | my $header = $list->Frame; | ||
| 167 | $header->Label(-text => $_)->pack(-side => 'left'); | ||
| 168 | $list->headerCreate($count, -headerbackground => $bg, -itemtype => 'window', -widget => $header); | ||
| 169 | # $list->headerCreate($count, -text => $_); | ||
| 170 | $count ++; | ||
| 171 | } | ||
| 172 | |||
| 173 | my $popcolor = $self->PopColor(-widget => ''); | ||
| 174 | for ($self->Theme->tagList) { | ||
| 175 | my $tag = $_; | ||
| 176 | |||
| 177 | $list->add($tag); | ||
| 178 | my $label = $list->Label( | ||
| 179 | -background => $defaultbackground, | ||
| 180 | -font => $defaultfont, | ||
| 181 | -anchor => 'e', | ||
| 182 | -text => $tag, | ||
| 183 | ); | ||
| 184 | $list->itemCreate($tag, 0, -itemtype => 'window', -widget => $label); | ||
| 185 | |||
| 186 | my $fg = $list->MyColorEntry( | ||
| 187 | -balloon => $balloon, | ||
| 188 | -width => 8, | ||
| 189 | -historyfile => $historyfile, | ||
| 190 | -command => ['updateForeground', $self, $tag], | ||
| 191 | -popcolor => $popcolor, | ||
| 192 | ); | ||
| 193 | $list->itemCreate($tag, 1, -itemtype => 'window', -widget => $fg); | ||
| 194 | |||
| 195 | my $bg = $list->MyColorEntry( | ||
| 196 | -balloon => $balloon, | ||
| 197 | -width => 8, | ||
| 198 | -historyfile => $historyfile, | ||
| 199 | -command => ['updateBackground', $self, $tag], | ||
| 200 | -popcolor => $popcolor, | ||
| 201 | ); | ||
| 202 | $list->itemCreate($tag, 2, -itemtype => 'window', -widget => $bg); | ||
| 203 | |||
| 204 | my $b = ''; | ||
| 205 | my $i = ''; | ||
| 206 | $self->Advertise($tag . "W", \$b); | ||
| 207 | my $bold = $list->Checkbutton( | ||
| 208 | -offvalue => '', | ||
| 209 | -onvalue => 'bold', | ||
| 210 | -command => sub { | ||
| 211 | $self->updateFont($tag, -weight => $b, -slant => $i); | ||
| 212 | }, | ||
| 213 | -variable => \$b, | ||
| 214 | ); | ||
| 215 | $list->itemCreate($tag, 3, -itemtype => 'window', -widget => $bold); | ||
| 216 | |||
| 217 | $self->Advertise($tag . "S", \$i); | ||
| 218 | my $italic = $list->Checkbutton( | ||
| 219 | -offvalue => '', | ||
| 220 | -onvalue => 'italic', | ||
| 221 | -command => sub { | ||
| 222 | $self->updateFont($tag, -weight => $b, -slant => $i); | ||
| 223 | }, | ||
| 224 | -variable => \$i, | ||
| 225 | ); | ||
| 226 | $list->itemCreate($tag, 4, -itemtype => 'window', -widget => $italic); | ||
| 227 | } | ||
| 228 | $self->ConfigSpecs( | ||
| 229 | -background => ['SELF', 'DESCENDANTS'], | ||
| 230 | -defaultbackground => ['PASSIVE', undef, undef, $defaultbackground], | ||
| 231 | -defaultforeground => ['PASSIVE', undef, undef, $defaultforeground], | ||
| 232 | -defaultfont => ['PASSIVE', undef, undef, $defaultfont], | ||
| 233 | DEFAULT => [ $list ], | ||
| 234 | ); | ||
| 235 | $self->Delegates( | ||
| 236 | DEFAULT => $list | ||
| 237 | ); | ||
| 238 | } | ||
| 239 | |||
| 240 | sub fontCompose { | ||
| 241 | my ($self, $font, %options) = @_; | ||
| 242 | my $family = $self->fontActual($font, '-family'); | ||
| 243 | my $size = $self->fontActual($font, '-size'); | ||
| 244 | my $weight = ''; | ||
| 245 | my $slant = ''; | ||
| 246 | $slant = $options{'-slant'} if exists $options{'-slant'}; | ||
| 247 | $weight = $options{'-weight'} if exists $options{'-weight'}; | ||
| 248 | $slant = 'roman' if $slant eq ''; | ||
| 249 | $weight = 'normal' if $weight eq ''; | ||
| 250 | return $self->Font( | ||
| 251 | -family => $family, | ||
| 252 | -size => $size, | ||
| 253 | -slant => $slant, | ||
| 254 | -weight => $weight, | ||
| 255 | ); | ||
| 256 | } | ||
| 257 | |||
| 258 | =item b<get> | ||
| 259 | |||
| 260 | Returns a list of tag/options pairs. | ||
| 261 | |||
| 262 | =cut | ||
| 263 | |||
| 264 | sub get { | ||
| 265 | my $self = shift; | ||
| 266 | return $self->Theme->get; | ||
| 267 | } | ||
| 268 | |||
| 269 | =item b<load> | ||
| 270 | |||
| 271 | Loads a CodeText theme definition file. | ||
| 272 | |||
| 273 | =cut | ||
| 274 | |||
| 275 | sub load { | ||
| 276 | my $self = shift; | ||
| 277 | $self->Theme->load(@_); | ||
| 278 | $self->updateAll; | ||
| 279 | } | ||
| 280 | |||
| 281 | =item b<put> | ||
| 282 | |||
| 283 | Assigns a @list of tag/option pairs. | ||
| 284 | |||
| 285 | =cut | ||
| 286 | |||
| 287 | sub put { | ||
| 288 | my $self = shift; | ||
| 289 | my $theme = $self->Theme; | ||
| 290 | $theme->put(@_); | ||
| 291 | for ($theme->tagList) { | ||
| 292 | my $tag = $_; | ||
| 293 | my $b = $self->itemCget($tag, 1, '-widget'); | ||
| 294 | $b->put($theme->getItem($tag, '-foreground')); | ||
| 295 | my $f = $self->itemCget($tag, 2, '-widget'); | ||
| 296 | $f->put($theme->getItem($tag, '-background')); | ||
| 297 | my $slant = $self->Subwidget($tag . 'S'); | ||
| 298 | $$slant = $theme->getItem($tag, '-slant'); | ||
| 299 | my $weight = $self->Subwidget($tag . 'W'); | ||
| 300 | $$weight = $theme->getItem($tag, '-weight'); | ||
| 301 | } | ||
| 302 | $self->updateAll; | ||
| 303 | } | ||
| 304 | |||
| 305 | =item B<save>I<($file)> | ||
| 306 | |||
| 307 | Saves a CodeText theme definition file. | ||
| 308 | |||
| 309 | =cut | ||
| 310 | |||
| 311 | sub save { | ||
| 312 | my $self = shift; | ||
| 313 | $self->Theme->save(@_); | ||
| 314 | } | ||
| 315 | |||
| 316 | sub updateAll { | ||
| 317 | my $self = shift; | ||
| 318 | my $theme = $self->Theme; | ||
| 319 | for ($theme->tagList) { | ||
| 320 | my $tag = $_; | ||
| 321 | my $bg = $theme->getItem($tag, '-background'); | ||
| 322 | $self->updateBackground($tag, $bg); | ||
| 323 | my $fg = $theme->getItem($tag, '-foreground'); | ||
| 324 | $self->updateForeground($tag, $fg); | ||
| 325 | my %font = ( | ||
| 326 | -slant => $theme->getItem($tag, '-slant'), | ||
| 327 | -weight => $theme->getItem($tag, '-weight'), | ||
| 328 | ); | ||
| 329 | $self->updateFont($tag, %font); | ||
| 330 | } | ||
| 331 | } | ||
| 332 | |||
| 333 | sub updateBackground { | ||
| 334 | my ($self, $tag, $color) = @_; | ||
| 335 | my $bg = $color; | ||
| 336 | $bg = $self->cget('-defaultbackground') if $color eq ''; | ||
| 337 | my $w = $self->itemCget($tag, 0, '-widget'); | ||
| 338 | $w->configure(-background => $bg); | ||
| 339 | $self->Theme->setItem($tag, '-background', $color); | ||
| 340 | } | ||
| 341 | |||
| 342 | sub updateFont { | ||
| 343 | my ($self, $tag, %values) = @_; | ||
| 344 | |||
| 345 | my $font = $self->cget('-defaultfont'); | ||
| 346 | my $weight = $self->fontActual($font, '-weight'); | ||
| 347 | my $slant = $self->fontActual($font, '-slant'); | ||
| 348 | my %options = ( | ||
| 349 | -slant => $self->fontActual($font, '-slant'), | ||
| 350 | -weight => $self->fontActual($font, '-weight'), | ||
| 351 | ); | ||
| 352 | for (keys %values) { | ||
| 353 | $options{$_} = $values{$_}; | ||
| 354 | $self->Theme->setItem($tag, $_, $values{$_}); | ||
| 355 | } | ||
| 356 | $font = $self->fontCompose($font, %options); | ||
| 357 | my $w = $self->itemCget($tag, 0, '-widget'); | ||
| 358 | $w->configure(-font => $font); | ||
| 359 | } | ||
| 360 | |||
| 361 | sub updateForeground { | ||
| 362 | my ($self, $tag, $color) = @_; | ||
| 363 | my $fg = $color; | ||
| 364 | $fg = $self->cget('-defaultforeground') if $color eq ''; | ||
| 365 | my $w = $self->itemCget($tag, 0, '-widget'); | ||
| 366 | $w->configure(-foreground =>$fg); | ||
| 367 | $self->Theme->setItem($tag, '-foreground',$color); | ||
| 368 | } | ||
| 369 | |||
| 370 | sub Theme { | ||
| 371 | return $_[0]->{THEME}; | ||
| 372 | } | ||
| 373 | |||
| 374 | =back | ||
| 375 | |||
| 376 | =head1 AUTHOR | ||
| 377 | |||
| 378 | Hans Jeuken (hanje at cpan dot org) | ||
| 379 | |||
| 380 | =head1 BUGS | ||
| 381 | |||
| 382 | Unknown. If you find any, please contact the author. | ||
| 383 | |||
| 384 | =head1 SEE ALSO | ||
| 385 | |||
| 386 | =over 4 | ||
| 387 | |||
| 388 | =item L<Tk::CodeText> | ||
| 389 | |||
| 390 | =item L<Tk::CodeText::Theme> | ||
| 391 | |||
| 392 | =back | ||
| 393 | |||
| 394 | =cut | ||
| 395 | |||
| 396 | 1; | ||
| 397 | |||
| 398 | __END__ |