| 1 | ################################################################################ | ||
| 2 | # This is CodeManager | ||
| 3 | # Copyright 2009-2013 by Waldemar Biernacki | ||
| 4 | # http://codemanager.sao.pl\n" . | ||
| 5 | # | ||
| 6 | # License statement: | ||
| 7 | # | ||
| 8 | # This program/library is free software; you can redistribute it | ||
| 9 | # and/or modify it under the same terms as Perl itself. | ||
| 10 | # | ||
| 11 | # Last modified (DMYhms): 13-01-2013 09:42:05. | ||
| 12 | ################################################################################ | ||
| 13 | |||
| 14 | package Prima::CodeManager::Label; | ||
| 15 | |||
| 16 | use strict; | ||
| 17 | use warnings; | ||
| 18 | |||
| 19 | use vars qw(@ISA); | ||
| 20 | @ISA = qw(Prima::Widget); | ||
| 21 | |||
| 22 | use Prima::Const; | ||
| 23 | use Prima::Classes; | ||
| 24 | |||
| 25 | sub profile_default | ||
| 26 | { | ||
| 27 | my $font = $_[ 0]-> get_default_font; | ||
| 28 | return { | ||
| 29 | %{$_[ 0]-> SUPER::profile_default}, | ||
| 30 | alignment => ta::Left, | ||
| 31 | autoHeight => 0, | ||
| 32 | autoWidth => 1, | ||
| 33 | focusLink => undef, | ||
| 34 | height => $font->{height}, | ||
| 35 | ownerBackColor => 1, | ||
| 36 | selectable => 0, | ||
| 37 | showAccelChar => 0, | ||
| 38 | showPartial => 1, | ||
| 39 | tabStop => 0, | ||
| 40 | valignment => ta::Top, | ||
| 41 | widgetClass => wc::Label, | ||
| 42 | wordWrap => 0, | ||
| 43 | lineSpace => 0, | ||
| 44 | } | ||
| 45 | } | ||
| 46 | |||
| 47 | sub profile_check_in | ||
| 48 | { | ||
| 49 | my ( $self, $p, $default) = @_; | ||
| 50 | $p-> { autoWidth} = 0 if | ||
| 51 | |||
| 52 | ! exists $p->{autoWidth} and ( | ||
| 53 | exists $p-> {width} || | ||
| 54 | |||
| 55 | exists $p-> {size} || | ||
| 56 | |||
| 57 | exists $p-> {rect} || | ||
| 58 | |||
| 59 | ( exists $p-> {left} && exists $p-> {right}) | ||
| 60 | ); | ||
| 61 | $p-> {autoHeight} = 0 if | ||
| 62 | |||
| 63 | ! exists $p-> {autoHeight} and ( | ||
| 64 | exists $p-> {height} || | ||
| 65 | |||
| 66 | exists $p-> {size} || | ||
| 67 | |||
| 68 | exists $p-> {rect} || | ||
| 69 | |||
| 70 | ( exists $p-> {top} && exists $p-> {bottom}) | ||
| 71 | ); | ||
| 72 | $self-> SUPER::profile_check_in( $p, $default); | ||
| 73 | my $vertical = exists $p-> {vertical} ? | ||
| 74 | |||
| 75 | $p-> {vertical} : | ||
| 76 | |||
| 77 | $default-> { vertical}; | ||
| 78 | |||
| 79 | $self->{lineSpace} = $p->{lineSpace} || 0; | ||
| 80 | } | ||
| 81 | |||
| 82 | sub init | ||
| 83 | { | ||
| 84 | my $self = shift; | ||
| 85 | my %profile = $self-> SUPER::init(@_); | ||
| 86 | $self-> { alignment} = $profile{ alignment}; | ||
| 87 | $self-> { valignment} = $profile{ valignment}; | ||
| 88 | $self-> { autoHeight} = $profile{ autoHeight}; | ||
| 89 | $self-> { autoWidth} = $profile{ autoWidth}; | ||
| 90 | $self-> { wordWrap} = $profile{ wordWrap}; | ||
| 91 | $self-> { focusLink} = $profile{ focusLink}; | ||
| 92 | $self-> { showAccelChar} = $profile{ showAccelChar}; | ||
| 93 | $self-> { showPartial} = $profile{ showPartial}; | ||
| 94 | $self-> { lineSpace} = $profile{ lineSpace}; | ||
| 95 | $self-> check_auto_size; | ||
| 96 | return %profile; | ||
| 97 | } | ||
| 98 | |||
| 99 | sub on_paint | ||
| 100 | { | ||
| 101 | my ($self,$canvas) = @_; | ||
| 102 | my @size = $canvas-> size; | ||
| 103 | my @clr; | ||
| 104 | if ( $self-> enabled) { | ||
| 105 | if ( $self-> focused) { | ||
| 106 | @clr = ($self-> hiliteColor, $self-> hiliteBackColor); | ||
| 107 | } else { | ||
| 108 | |||
| 109 | @clr = ($self-> color, $self-> backColor); | ||
| 110 | |||
| 111 | } | ||
| 112 | } else { | ||
| 113 | |||
| 114 | @clr = ($self-> disabledColor, $self-> disabledBackColor); | ||
| 115 | |||
| 116 | } | ||
| 117 | |||
| 118 | unless ( $self-> transparent) { | ||
| 119 | $canvas-> color( $clr[1]); | ||
| 120 | $canvas-> bar(0,0,@size); | ||
| 121 | } | ||
| 122 | |||
| 123 | my $fh = $canvas-> font-> height + $self->{lineSpace}; | ||
| 124 | my $ta = $self-> {alignment}; | ||
| 125 | my $wx = $self-> {widths}; | ||
| 126 | my $ws = $self-> {words}; | ||
| 127 | my ($starty,$ycommon) = (0, scalar @{$ws} * $fh); | ||
| 128 | |||
| 129 | if ( $self-> {valignment} == ta::Top) { | ||
| 130 | |||
| 131 | $starty = $size[1] - $fh; | ||
| 132 | } elsif ( $self-> {valignment} == ta::Bottom) { | ||
| 133 | |||
| 134 | $starty = $ycommon - $fh; | ||
| 135 | } else { | ||
| 136 | |||
| 137 | $starty = ( $size[1] + $ycommon)/2 - $fh; | ||
| 138 | |||
| 139 | } | ||
| 140 | |||
| 141 | my $y = $starty; | ||
| 142 | my $tl = $self-> {tildeLine}; | ||
| 143 | my $i; | ||
| 144 | my $paintLine = !$self-> {showAccelChar} && defined($tl) && $tl < scalar @{$ws}; | ||
| 145 | |||
| 146 | unless ( $self-> enabled) { | ||
| 147 | $canvas-> color( $self-> light3DColor); | ||
| 148 | for ( $i = 0; $i < scalar @{$ws}; $i++) { | ||
| 149 | my $x = 0; | ||
| 150 | if ( $ta == ta::Center) { | ||
| 151 | |||
| 152 | $x = ( $size[0] - $$wx[$i]) / 2; | ||
| 153 | |||
| 154 | } elsif ( $ta == ta::Right) { | ||
| 155 | |||
| 156 | $x = $size[0] - $$wx[$i]; | ||
| 157 | |||
| 158 | } | ||
| 159 | $canvas-> text_out( $$ws[$i], $x + 1, $y - 1); | ||
| 160 | $y -= $fh; | ||
| 161 | } | ||
| 162 | $y = $starty; | ||
| 163 | if ( $paintLine) { | ||
| 164 | my $x = 0; | ||
| 165 | if ( $ta == ta::Center) { | ||
| 166 | |||
| 167 | $x = ( $size[0] - $$wx[$tl]) / 2; | ||
| 168 | |||
| 169 | } elsif ( $ta == ta::Right) { | ||
| 170 | |||
| 171 | $x = $size[0] - $$wx[$tl]; | ||
| 172 | |||
| 173 | } | ||
| 174 | $canvas-> line( | ||
| 175 | |||
| 176 | $x + $self-> {tildeStart} + 1, $starty - $fh * $tl - 1, | ||
| 177 | $x + $self-> {tildeEnd} + 1, $starty - $fh * $tl - 1 | ||
| 178 | ); | ||
| 179 | } | ||
| 180 | } | ||
| 181 | |||
| 182 | $canvas-> color( $clr[0]); | ||
| 183 | for ( $i = 0; $i < scalar @{$ws}; $i++) { | ||
| 184 | my $x = 0; | ||
| 185 | if ( $ta == ta::Center) { | ||
| 186 | |||
| 187 | $x = ( $size[0] - $$wx[$i]) / 2; | ||
| 188 | |||
| 189 | } elsif ( $ta == ta::Right) { | ||
| 190 | |||
| 191 | $x = $size[0] - $$wx[$i]; | ||
| 192 | |||
| 193 | } | ||
| 194 | $canvas-> text_out( $$ws[$i], $x, $y); | ||
| 195 | $y -= $fh; | ||
| 196 | } | ||
| 197 | if ( $paintLine) { | ||
| 198 | my $x = 0; | ||
| 199 | if ( $ta == ta::Center) { $x = ( $size[0] - $$wx[$tl]) / 2; } | ||
| 200 | elsif ( $ta == ta::Right) { $x = $size[0] - $$wx[$tl]; } | ||
| 201 | $canvas-> line( | ||
| 202 | |||
| 203 | $x + $self-> {tildeStart}, $starty - $fh * $tl, | ||
| 204 | $x + $self-> {tildeEnd}, $starty - $fh * $tl | ||
| 205 | ); | ||
| 206 | } | ||
| 207 | } | ||
| 208 | |||
| 209 | sub text | ||
| 210 | { | ||
| 211 | return $_[0]-> SUPER::text unless $#_; | ||
| 212 | my $self = $_[0]; | ||
| 213 | $self-> SUPER::text( $_[1]); | ||
| 214 | $self-> check_auto_size; | ||
| 215 | $self-> repaint; | ||
| 216 | } | ||
| 217 | |||
| 218 | sub on_translateaccel | ||
| 219 | { | ||
| 220 | my ( $self, $code, $key, $mod) = @_; | ||
| 221 | if ( | ||
| 222 | |||
| 223 | !$self-> {showAccelChar} && | ||
| 224 | |||
| 225 | defined $self-> {accel} && | ||
| 226 | |||
| 227 | ( $key == kb::NoKey) && | ||
| 228 | |||
| 229 | lc chr $code eq $self-> { accel} | ||
| 230 | ) { | ||
| 231 | $self-> clear_event; | ||
| 232 | $self-> notify( 'Click'); | ||
| 233 | } | ||
| 234 | } | ||
| 235 | |||
| 236 | sub on_click | ||
| 237 | { | ||
| 238 | my ( $self, $f) = ( $_[0], $_[0]-> {focusLink}); | ||
| 239 | $f-> select if defined $f && $f-> alive && $f-> enabled; | ||
| 240 | } | ||
| 241 | |||
| 242 | sub on_keydown | ||
| 243 | { | ||
| 244 | my ( $self, $code, $key, $mod) = @_; | ||
| 245 | if ( | ||
| 246 | |||
| 247 | defined $self-> {accel} && | ||
| 248 | |||
| 249 | ( $key == kb::NoKey) && | ||
| 250 | |||
| 251 | lc chr $code eq $self-> { accel} | ||
| 252 | ) { | ||
| 253 | $self-> notify( 'Click'); | ||
| 254 | $self-> clear_event; | ||
| 255 | } | ||
| 256 | } | ||
| 257 | |||
| 258 | sub on_mousedown | ||
| 259 | { | ||
| 260 | my $self = $_[0]; | ||
| 261 | $self-> notify( 'Click'); | ||
| 262 | $self-> clear_event; | ||
| 263 | } | ||
| 264 | |||
| 265 | sub on_fontchanged | ||
| 266 | { | ||
| 267 | $_[0]-> check_auto_size; | ||
| 268 | } | ||
| 269 | |||
| 270 | sub on_size | ||
| 271 | { | ||
| 272 | $_[0]-> reset_lines; | ||
| 273 | } | ||
| 274 | |||
| 275 | sub on_enable { $_[0]-> repaint } sub on_disable { $_[0]-> repaint } | ||
| 276 | |||
| 277 | sub set_alignment | ||
| 278 | { | ||
| 279 | $_[0]-> {alignment} = $_[1]; | ||
| 280 | $_[0]-> repaint; | ||
| 281 | } | ||
| 282 | |||
| 283 | sub set_valignment | ||
| 284 | { | ||
| 285 | $_[0]-> {valignment} = $_[1]; | ||
| 286 | $_[0]-> repaint; | ||
| 287 | } | ||
| 288 | |||
| 289 | sub reset_lines | ||
| 290 | { | ||
| 291 | my $self = $_[0]; | ||
| 292 | |||
| 293 | my @res; | ||
| 294 | my $maxLines = int( $self-> height / ($self-> font-> height + $self->{lineSpace})); | ||
| 295 | $maxLines++ if $self-> {showPartial} and (($self-> height % ($self-> font-> height + $self->{lineSpace})) > 0); | ||
| 296 | |||
| 297 | my $opt = tw::NewLineBreak|tw::ReturnLines|tw::WordBreak|tw::CalcMnemonic|tw::ExpandTabs|tw::CalcTabs; | ||
| 298 | my $width = 1000000; | ||
| 299 | $opt |= tw::CollapseTilde unless $self-> {showAccelChar}; | ||
| 300 | $width = $self-> width if $self-> {wordWrap}; | ||
| 301 | |||
| 302 | $self-> begin_paint_info; | ||
| 303 | |||
| 304 | my $lines = $self-> text_wrap( $self-> text, $width, $opt); | ||
| 305 | my $lastRef = pop @{$lines}; | ||
| 306 | |||
| 307 | $self-> {textLines} = scalar @$lines; | ||
| 308 | for( qw( tildeStart tildeEnd tildeLine)) {$self-> {$_} = $lastRef-> {$_}} | ||
| 309 | |||
| 310 | $self-> {accel} = defined($self-> {tildeStart}) ? lc( $lastRef-> {tildeChar}) : undef; | ||
| 311 | splice( @{$lines}, $maxLines) if scalar @{$lines} > $maxLines; | ||
| 312 | $self-> {words} = $lines; | ||
| 313 | |||
| 314 | my @len; | ||
| 315 | for ( @{$lines}) { push @len, $self-> get_text_width( $_); } | ||
| 316 | $self-> {widths} = [@len]; | ||
| 317 | |||
| 318 | $self-> end_paint_info; | ||
| 319 | } | ||
| 320 | |||
| 321 | sub check_auto_size | ||
| 322 | { | ||
| 323 | my $self = $_[0]; | ||
| 324 | my $cap = $self-> text; | ||
| 325 | $cap =~ s/~//s unless $self-> {showAccelChar}; | ||
| 326 | my %sets; | ||
| 327 | |||
| 328 | if ( $self-> {wordWrap}) { | ||
| 329 | $self-> reset_lines; | ||
| 330 | if ( $self-> {autoHeight}) { | ||
| 331 | $self-> geomHeight( $self-> {textLines} * ($self-> font-> height + $self->{lineSpace}) + 2); | ||
| 332 | } | ||
| 333 | } else { | ||
| 334 | my @lines = split "\n", $cap; | ||
| 335 | if ( $self-> {autoWidth}) { | ||
| 336 | $self-> begin_paint_info; | ||
| 337 | $sets{geomWidth} = 0; | ||
| 338 | for my $line ( @lines) { | ||
| 339 | my $width = $self-> get_text_width( $line); | ||
| 340 | $sets{geomWidth} = $width if | ||
| 341 | |||
| 342 | $sets{geomWidth} < $width; | ||
| 343 | } | ||
| 344 | $sets{geomWidth} += 6; | ||
| 345 | $self-> end_paint_info; | ||
| 346 | } | ||
| 347 | $sets{ geomHeight} = scalar(@lines) * ($self-> font-> height + $self->{lineSpace}) + 2 | ||
| 348 | |||
| 349 | if $self-> {autoHeight}; | ||
| 350 | $self-> set( %sets); | ||
| 351 | $self-> reset_lines; | ||
| 352 | } | ||
| 353 | } | ||
| 354 | |||
| 355 | sub set_auto_width | ||
| 356 | { | ||
| 357 | my ( $self, $aw) = @_; | ||
| 358 | return if $self-> {autoWidth} == $aw; | ||
| 359 | $self-> {autoWidth} = $aw; | ||
| 360 | $self-> check_auto_size; | ||
| 361 | } | ||
| 362 | |||
| 363 | sub set_auto_height | ||
| 364 | { | ||
| 365 | my ( $self, $ah) = @_; | ||
| 366 | return if $self-> {autoHeight} == $ah; | ||
| 367 | $self-> {autoHeight} = $ah; | ||
| 368 | $self-> check_auto_size; | ||
| 369 | } | ||
| 370 | |||
| 371 | sub set_word_wrap | ||
| 372 | { | ||
| 373 | my ( $self, $ww) = @_; | ||
| 374 | return if $self-> {wordWrap} == $ww; | ||
| 375 | $self-> {wordWrap} = $ww; | ||
| 376 | $self-> check_auto_size; | ||
| 377 | } | ||
| 378 | |||
| 379 | sub set_show_accel_char | ||
| 380 | { | ||
| 381 | my ( $self, $sac) = @_; | ||
| 382 | return if $self-> {showAccelChar} == $sac; | ||
| 383 | $self-> {showAccelChar} = $sac; | ||
| 384 | $self-> check_auto_size; | ||
| 385 | } | ||
| 386 | |||
| 387 | sub set_show_partial | ||
| 388 | { | ||
| 389 | my ( $self, $sp) = @_; | ||
| 390 | return if $self-> {showPartial} == $sp; | ||
| 391 | $self-> {showPartial} = $sp; | ||
| 392 | $self-> check_auto_size; | ||
| 393 | } | ||
| 394 | |||
| 395 | sub get_lines | ||
| 396 | { | ||
| 397 | return @{$_[0]-> {words}}; | ||
| 398 | } | ||
| 399 | |||
| 400 | sub showAccelChar {($#_)?($_[0]-> set_show_accel_char($_[1])) :return $_[0]-> {showAccelChar}} | ||
| 401 | sub showPartial {($#_)?($_[0]-> set_show_partial($_[1])) :return $_[0]-> {showPartial}} | ||
| 402 | sub focusLink {($#_)?($_[0]-> {focusLink} = $_[1]) :return $_[0]-> {focusLink} } | ||
| 403 | sub alignment {($#_)?($_[0]-> set_alignment( $_[1])) :return $_[0]-> {alignment} } | ||
| 404 | sub valignment {($#_)?($_[0]-> set_valignment( $_[1])) :return $_[0]-> {valignment} } | ||
| 405 | sub autoWidth {($#_)?($_[0]-> set_auto_width( $_[1])) :return $_[0]-> {autoWidth} } | ||
| 406 | sub autoHeight {($#_)?($_[0]-> set_auto_height( $_[1])) :return $_[0]-> {autoHeight} } | ||
| 407 | sub wordWrap {($#_)?($_[0]-> set_word_wrap( $_[1])) :return $_[0]-> {wordWrap} } | ||
| 408 | |||
| 409 | 1; | ||
| 410 | |||
| 411 | __END__ |