| 1 | package Tk::AppWindow; | ||
| 2 | |||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | Tk::AppWindow - An application framework based on Tk | ||
| 6 | |||
| 7 | =cut | ||
| 8 | |||
| 9 | use strict; | ||
| 10 | use warnings; | ||
| 11 | use Carp; | ||
| 12 | use vars qw($VERSION); | ||
| 13 | $VERSION="0.24"; | ||
| 14 | |||
| 15 | use base qw(Tk::Derived Tk::MainWindow); | ||
| 16 | Construct Tk::Widget 'AppWindow'; | ||
| 17 | |||
| 18 | use File::Basename; | ||
| 19 | require Tk::AppWindow::BaseClasses::Callback; | ||
| 20 | require Tk::Balloon; | ||
| 21 | require Tk::DynaMouseWheelBind; | ||
| 22 | require Tk::FilePicker; | ||
| 23 | require Tk::QuickForm; | ||
| 24 | require Tk::YAMessage; | ||
| 25 | use Tk::PNG; | ||
| 26 | |||
| 27 | |||
| 28 | use Config; | ||
| 29 | my $mswin = 0; | ||
| 30 | $mswin = 1 if $Config{'osname'} eq 'MSWin32'; | ||
| 31 | |||
| 32 | =head1 SYNOPSIS | ||
| 33 | |||
| 34 | my $app = new Tk::AppWindow(@options, | ||
| 35 | -extensions => ['ConfigFolder'], | ||
| 36 | ); | ||
| 37 | $app->MainLoop; | ||
| 38 | |||
| 39 | =head1 DESCRIPTION | ||
| 40 | |||
| 41 | An extendable application framework written in perl/Tk. The aim is maximum user configurability | ||
| 42 | and ease of application building. | ||
| 43 | |||
| 44 | To get started read L<Tk::AppWindow::OverView> and L<Tk::AppWindow::CookBook>. | ||
| 45 | |||
| 46 | This document is a reference manual. | ||
| 47 | |||
| 48 | =head1 CONFIG VARIABLES | ||
| 49 | |||
| 50 | =over 4 | ||
| 51 | |||
| 52 | =item Switch: B<-appname> | ||
| 53 | |||
| 54 | Set the name of your application. | ||
| 55 | |||
| 56 | If this option is not specified, the name of your application | ||
| 57 | will be set to the filename of your executable with the first | ||
| 58 | character in upper case. | ||
| 59 | |||
| 60 | =item Switch: B<-commands> | ||
| 61 | |||
| 62 | Defines commands to be used in your application. It takes a paired list of | ||
| 63 | command names and callbacks as parameter. | ||
| 64 | |||
| 65 | my $app = $k::AppWindw->new( | ||
| 66 | -commands => [ | ||
| 67 | do_something1 => ['method', $obj], | ||
| 68 | do_something2 => sub { return 1 }, | ||
| 69 | ], | ||
| 70 | ); | ||
| 71 | |||
| 72 | Only available at create time. | ||
| 73 | |||
| 74 | =item Name : B<errorColor> | ||
| 75 | |||
| 76 | =item Class : B<ErrorColor> | ||
| 77 | |||
| 78 | =item Switch: B<-errorcolor> | ||
| 79 | |||
| 80 | Default value '#FF0000' (red). | ||
| 81 | |||
| 82 | =item Switch: B<-extensions> | ||
| 83 | |||
| 84 | Specifies the list of extensions to be loaded. | ||
| 85 | |||
| 86 | my $app = Tk::AppWindow->new( | ||
| 87 | -extensions => [ | ||
| 88 | qw/Art ConfigFolder Daemons | ||
| 89 | Help Keyboard MDI MenuBar | ||
| 90 | Panels Plugins SDI Selector | ||
| 91 | Settings SideBars StatusBar | ||
| 92 | ToolBar/ | ||
| 93 | ], | ||
| 94 | ); | ||
| 95 | |||
| 96 | The following order matters for the buildup of menus and bars. | ||
| 97 | Only available at create time. | ||
| 98 | |||
| 99 | =item Name : B<linkColor> | ||
| 100 | |||
| 101 | =item Class : B<LinkColor> | ||
| 102 | |||
| 103 | =item Switch: B<-linkcolor> | ||
| 104 | |||
| 105 | Foreground color for links. Default value '#3030DF'. | ||
| 106 | |||
| 107 | =item Switch: B<-logcall> | ||
| 108 | |||
| 109 | Callback to log messages. | ||
| 110 | |||
| 111 | =item Switch: B<-logerrorcall> | ||
| 112 | |||
| 113 | Callback to log errors. | ||
| 114 | |||
| 115 | =item Switch: B<-logwarningcall> | ||
| 116 | |||
| 117 | Callback to log warnings. | ||
| 118 | |||
| 119 | =item Switch: B<-logo> | ||
| 120 | |||
| 121 | Specifies the image file to be used as logo for your application. | ||
| 122 | Default value is Tk::findINC('Tk/AppWindow/aw_logo.png'). | ||
| 123 | |||
| 124 | =item Switch: B<-namespace> | ||
| 125 | |||
| 126 | Specifies an additional name space for extensions and plugins. | ||
| 127 | If you set it, for example, to 'Foo::Bar', then your extensions | ||
| 128 | may also live in 'Foo::Bar::Ext' and your plugins may live | ||
| 129 | in 'Foo::Bar::Plugins'. | ||
| 130 | |||
| 131 | Only available at create time. | ||
| 132 | |||
| 133 | =item Switch: B<-savegeometry> | ||
| 134 | |||
| 135 | Default value 0. Saves the geometry on quit and loads it on start. Only works | ||
| 136 | if the extension B<ConfigFolder> is loaded. | ||
| 137 | |||
| 138 | =back | ||
| 139 | |||
| 140 | =head1 COMMANDS | ||
| 141 | |||
| 142 | =over 4 | ||
| 143 | |||
| 144 | =item B<quit> | ||
| 145 | |||
| 146 | Calls the CmdQuit method. See there. | ||
| 147 | |||
| 148 | =back | ||
| 149 | |||
| 150 | =cut | ||
| 151 | |||
| 152 | sub Populate { | ||
| 153 | my ($self,$args) = @_; | ||
| 154 | |||
| 155 | my $commands = delete $args->{'-commands'}; | ||
| 156 | $commands = [] unless defined $commands; | ||
| 157 | |||
| 158 | my $extensions = delete $args->{'-extensions'}; | ||
| 159 | $extensions = [] unless defined $extensions; | ||
| 160 | |||
| 161 | my $namespace = delete $args->{'-namespace'}; | ||
| 162 | |||
| 163 | my $preconfig = delete $args->{'-preconfig'}; | ||
| 164 | $preconfig = [] unless defined $preconfig; | ||
| 165 | |||
| 166 | my $appname = delete $args->{'-appname'}; | ||
| 167 | $appname = ucfirst(basename($0, '.pl', '.PL')) unless defined $appname; | ||
| 168 | $args->{'-title'} = $appname; | ||
| 169 | |||
| 170 | $self->SUPER::Populate($args); | ||
| 171 | |||
| 172 | $self->DynaMouseWheelBind( | ||
| 173 | 'Tk::Canvas', | ||
| 174 | 'Tk::HList', | ||
| 175 | 'Tk::ITree', | ||
| 176 | 'Tk::Pane', | ||
| 177 | 'Tk::Tree', | ||
| 178 | ); | ||
| 179 | |||
| 180 | $self->{APPNAME} = $appname; | ||
| 181 | $self->{ARGS} = $args; | ||
| 182 | $self->{BALLOON} = $self->Balloon; | ||
| 183 | $self->{CMNDTABLE} = {}; | ||
| 184 | $self->{CONFIGTABLE} = {}; | ||
| 185 | $self->{GEOBLOCK} = 0; | ||
| 186 | $self->{GEOCALLS} = {}; | ||
| 187 | $self->{GEOEXCLUSIVE} = ''; | ||
| 188 | $self->{EXTENSIONS} = {}; | ||
| 189 | $self->{EXTLOADORDER} = []; | ||
| 190 | $self->{LOGDISABLED} = 0; | ||
| 191 | $self->{NAMESPACE} = $namespace; | ||
| 192 | $self->{WORKSPACE} = $self; | ||
| 193 | |||
| 194 | $self->cmdConfig( | ||
| 195 | poptest => ['popTest', $self], #usefull for testing only | ||
| 196 | quit => ['CmdQuit', $self], | ||
| 197 | @$commands | ||
| 198 | ); | ||
| 199 | $self->configInit( | ||
| 200 | -appname => ['appName', $self, $appname], | ||
| 201 | ); | ||
| 202 | |||
| 203 | $self->{POSTCONFIG} = []; | ||
| 204 | $self->{PRECONFIG} = $preconfig; | ||
| 205 | for (@$extensions) { | ||
| 206 | $self->extLoad($_, $args); | ||
| 207 | } | ||
| 208 | |||
| 209 | my $setplug = $self->extGet('Settings'); | ||
| 210 | if (defined $setplug) { | ||
| 211 | my @useroptions = $setplug->LoadSettings; | ||
| 212 | my $tab = $self->{CONFIGTABLE}; | ||
| 213 | while (@useroptions) { | ||
| 214 | my $option = shift @useroptions; | ||
| 215 | my $value = shift @useroptions; | ||
| 216 | if (exists $tab->{$option}) { | ||
| 217 | $self->configPut($option, $value) | ||
| 218 | } else { | ||
| 219 | $args->{$option} = $value; | ||
| 220 | } | ||
| 221 | } | ||
| 222 | } | ||
| 223 | |||
| 224 | |||
| 225 | my $pre = $self->{PRECONFIG}; | ||
| 226 | my $logcall = sub { | ||
| 227 | my $message = shift; | ||
| 228 | print STDERR "$message\n"; | ||
| 229 | }; | ||
| 230 | $self->ConfigSpecs( | ||
| 231 | -compactviewsize => ['PASSIVE', undef, undef, 22], | ||
| 232 | -detailedviewsize => ['PASSIVE', undef, undef, 16], | ||
| 233 | -iconviewsize =>['PASSIVE', undef, undef, 128], | ||
| 234 | |||
| 235 | -linkcolor => ['PASSIVE', 'linkColor', 'LinkColor', '#3030DF'], | ||
| 236 | -logcall => ['CALLBACK', undef, undef, $logcall], | ||
| 237 | -logerrorcall => ['CALLBACK', undef, undef, $logcall], | ||
| 238 | -logwarningcall => ['CALLBACK', undef, undef, $logcall], | ||
| 239 | -logo => ['PASSIVE', undef, undef, Tk::findINC('Tk/AppWindow/aw_logo.png')], | ||
| 240 | -savegeometry => ['PASSIVE', undef, undef, 0], | ||
| 241 | @$pre, | ||
| 242 | DEFAULT => ['SELF'], | ||
| 243 | ); | ||
| 244 | |||
| 245 | $self->Delegates( | ||
| 246 | DEFAULT => $self, | ||
| 247 | ); | ||
| 248 | |||
| 249 | $self->protocol('WM_DELETE_WINDOW', ['CmdQuit', $self]); | ||
| 250 | delete $self->{ARGS}; | ||
| 251 | $self->after(1, ['PostConfig', $self]); | ||
| 252 | } | ||
| 253 | |||
| 254 | =head1 METHODS | ||
| 255 | |||
| 256 | =over 4 | ||
| 257 | |||
| 258 | =item B<abbreviate>I<($string, ?$size?, ?$firstsize?)> | ||
| 259 | |||
| 260 | Shortens $string to $size by leaving out a middle part. Then returns it. | ||
| 261 | $size is set to 30 unless you specify it. $firstsize is set to 25% of $size | ||
| 262 | unless you set it. | ||
| 263 | |||
| 264 | =cut | ||
| 265 | |||
| 266 | sub abbreviate { | ||
| 267 | my ($self, $string, $size, $firstsize) = @_; | ||
| 268 | $size = 30 unless defined $size; | ||
| 269 | $firstsize = int($size / 4) unless defined $firstsize; | ||
| 270 | my $length = length($string); | ||
| 271 | if ($length > $size) { | ||
| 272 | my $first = substr($string, 0, $firstsize) . ' ... '; | ||
| 273 | my $lastsize = $size - $firstsize - 5; | ||
| 274 | my $last = substr($string, $length - $lastsize, $lastsize); | ||
| 275 | $string = $first . $last; | ||
| 276 | } | ||
| 277 | return $string; | ||
| 278 | } | ||
| 279 | |||
| 280 | =item B<addPostConfig>I<('Method', $obj, @options)> | ||
| 281 | |||
| 282 | Only to be called by extensions at create time. | ||
| 283 | Specifies a callback te be executed after main loop starts. | ||
| 284 | |||
| 285 | Callbacks are executed in the order they are added. | ||
| 286 | |||
| 287 | =cut | ||
| 288 | |||
| 289 | sub addPostConfig { | ||
| 290 | my $self = shift; | ||
| 291 | my $pc = $self->{POSTCONFIG}; | ||
| 292 | my $call = $self->CreateCallback(@_); | ||
| 293 | push @$pc, $call | ||
| 294 | } | ||
| 295 | |||
| 296 | =item B<addPreConfig>I<(@configs)> | ||
| 297 | |||
| 298 | Only to be called by extensions at create time. | ||
| 299 | Specifies configs to the ConfigSpec method executed in Populate. | ||
| 300 | |||
| 301 | =cut | ||
| 302 | |||
| 303 | sub addPreConfig { | ||
| 304 | my $self = shift; | ||
| 305 | my $p = $self->{PRECONFIG}; | ||
| 306 | push @$p, @_ | ||
| 307 | } | ||
| 308 | |||
| 309 | =item B<appName>I<($name)> | ||
| 310 | |||
| 311 | Sets and returns the application name. | ||
| 312 | Same as $app->configPut(-name => $name), or $app->configGet($name). | ||
| 313 | |||
| 314 | =cut | ||
| 315 | |||
| 316 | sub appName { | ||
| 317 | my $self = shift; | ||
| 318 | if (@_) { $self->{APPNAME} = shift } | ||
| 319 | return $self->{APPNAME} | ||
| 320 | } | ||
| 321 | |||
| 322 | =item B<balloon> | ||
| 323 | |||
| 324 | Returns a reference to the Balloon widget. | ||
| 325 | |||
| 326 | =cut | ||
| 327 | |||
| 328 | sub balloon { | ||
| 329 | return $_[0]->{BALLOON} | ||
| 330 | } | ||
| 331 | |||
| 332 | =item B<BalloonAttach>I<$widget, $text> | ||
| 333 | |||
| 334 | Attaches a balloon message to $widget. | ||
| 335 | |||
| 336 | =cut | ||
| 337 | |||
| 338 | sub BalloonAttach { | ||
| 339 | my ($self, $widget, $text) = @_; | ||
| 340 | $self->{BALLOON}->attach($widget, -balloonmsg => $text) | ||
| 341 | } | ||
| 342 | |||
| 343 | =item B<CanQuit> | ||
| 344 | |||
| 345 | Returns 1. It is called when Tk::AppWindow tests all extensions if they can quit. You can | ||
| 346 | overwrite it when you inherit Tk::AppWindow. | ||
| 347 | |||
| 348 | =cut | ||
| 349 | |||
| 350 | sub CanQuit { | ||
| 351 | my $self = shift; | ||
| 352 | return 1 | ||
| 353 | } | ||
| 354 | |||
| 355 | sub CmdQuit { | ||
| 356 | my $self = shift; | ||
| 357 | my $quit = 1; | ||
| 358 | my $exts = $self->{EXTENSIONS}; | ||
| 359 | for (keys %$exts) { | ||
| 360 | $quit = 0 unless $exts->{$_}->CanQuit; | ||
| 361 | } | ||
| 362 | $quit = 0 unless $self->CanQuit; | ||
| 363 | if ($quit) { | ||
| 364 | #saving geometry | ||
| 365 | if ($self->configGet('-savegeometry')) { | ||
| 366 | my $cf = $self->extGet('ConfigFolder'); | ||
| 367 | if (defined $cf) { | ||
| 368 | my $geometry = $self->geometry; | ||
| 369 | $cf->saveList('geometry', "aw geometry", $geometry); | ||
| 370 | } | ||
| 371 | } | ||
| 372 | #quitting extensions | ||
| 373 | for (keys %$exts) { | ||
| 374 | $exts->{$_}->Quit; | ||
| 375 | } | ||
| 376 | $self->destroy; | ||
| 377 | } | ||
| 378 | } | ||
| 379 | |||
| 380 | =item B<cmdConfig>I<(@commands)> | ||
| 381 | |||
| 382 | $app->cmdConfig( | ||
| 383 | command1 => ['SomeMethod', $obj, @options], | ||
| 384 | command2 => [sub { do whatever }, @options], | ||
| 385 | ); | ||
| 386 | |||
| 387 | cmdConfig takes a paired list of commandnames and callback descriptions. | ||
| 388 | It registers them to the commands table. After that B<cmdExecute> can | ||
| 389 | be called on them. | ||
| 390 | |||
| 391 | =cut | ||
| 392 | |||
| 393 | sub cmdConfig { | ||
| 394 | my $self = shift; | ||
| 395 | my $tbl = $self->{CMNDTABLE}; | ||
| 396 | while (@_) { | ||
| 397 | my $key = shift; | ||
| 398 | my $callback = shift; | ||
| 399 | unless (exists $tbl->{$key}) { | ||
| 400 | $tbl->{$key} = $self->CreateCallback(@$callback); | ||
| 401 | } else { | ||
| 402 | carp "Command $key already exists" | ||
| 403 | } | ||
| 404 | } | ||
| 405 | } | ||
| 406 | |||
| 407 | =item B<cmdExecute>('command_name', @options); | ||
| 408 | |||
| 409 | Looks for the callback assigned to command_name and executes it. | ||
| 410 | returns the result. | ||
| 411 | |||
| 412 | =cut | ||
| 413 | |||
| 414 | sub cmdExecute { | ||
| 415 | my $self = shift; | ||
| 416 | my $key = shift; | ||
| 417 | my $cmd = $self->{CMNDTABLE}->{$key}; | ||
| 418 | if (defined $cmd) { | ||
| 419 | return $cmd->execute(@_); | ||
| 420 | } else { | ||
| 421 | carp "Command $key is not defined" | ||
| 422 | } | ||
| 423 | } | ||
| 424 | |||
| 425 | =item B<cmdExists>('command_name') | ||
| 426 | |||
| 427 | Checks if command_name can be used as a command. Returns a boolean. | ||
| 428 | |||
| 429 | =cut | ||
| 430 | |||
| 431 | sub cmdExists { | ||
| 432 | my ($self, $key) = @_; | ||
| 433 | unless (defined $key) { return 0 } | ||
| 434 | return exists $self->{CMNDTABLE}->{$key}; | ||
| 435 | } | ||
| 436 | |||
| 437 | sub CmdGet { | ||
| 438 | my ($self, $cmd) = @_; | ||
| 439 | return $self->{CMNDTABLE}->{$cmd} | ||
| 440 | } | ||
| 441 | |||
| 442 | sub CmdHook { | ||
| 443 | my $self = shift; | ||
| 444 | my $method = shift; | ||
| 445 | my $cmd = shift; | ||
| 446 | my $call = $self->CmdGet($cmd); | ||
| 447 | if (defined $call) { | ||
| 448 | $call->$method(@_); | ||
| 449 | return | ||
| 450 | } | ||
| 451 | carp "Command '$cmd' does not exist" | ||
| 452 | } | ||
| 453 | |||
| 454 | =item B<cmdHookAfter>(I<'command_name'>, I<@callback>) | ||
| 455 | |||
| 456 | Adds a hook to after stack of the callback associated with 'command_name'. | ||
| 457 | See L<Tk::AppWindow::BaseClasses::Callback>. | ||
| 458 | |||
| 459 | =cut | ||
| 460 | |||
| 461 | sub cmdHookAfter { | ||
| 462 | my $self = shift; | ||
| 463 | return $self->CmdHook('hookAfter', @_); | ||
| 464 | } | ||
| 465 | |||
| 466 | =item B<cmdHookBefore>(I<'command_name'>, I<@callback>) | ||
| 467 | |||
| 468 | Adds a hook to before stack of the callback associated with 'command_name'. | ||
| 469 | See L<Tk::AppWindow::BaseClasses::Callback>. | ||
| 470 | |||
| 471 | =cut | ||
| 472 | |||
| 473 | sub cmdHookBefore { | ||
| 474 | my $self = shift; | ||
| 475 | return $self->CmdHook('hookBefore', @_); | ||
| 476 | } | ||
| 477 | |||
| 478 | |||
| 479 | =item B<cmdRemove>(I<'command_name'>) | ||
| 480 | |||
| 481 | Removes 'command_name' from the command stack. | ||
| 482 | |||
| 483 | =cut | ||
| 484 | |||
| 485 | sub cmdRemove { | ||
| 486 | my ($self, $key) = @_; | ||
| 487 | return unless defined $key; | ||
| 488 | return delete $self->{CMNDTABLE}->{$key}; | ||
| 489 | } | ||
| 490 | |||
| 491 | =item B<cmdUnhookAfter>(I<'command_name'>, I<@callback>) | ||
| 492 | |||
| 493 | unhooks a hook from after stack of the callback associated with 'command_name'. | ||
| 494 | See L<Tk::AppWindow::BaseClasses::Callback>. | ||
| 495 | |||
| 496 | =cut | ||
| 497 | |||
| 498 | sub cmdUnhookAfter { | ||
| 499 | my $self = shift; | ||
| 500 | return $self->CmdHook('unhookAfter', @_); | ||
| 501 | } | ||
| 502 | |||
| 503 | =item B<cmdUnhookBefore>(I<'command_name'>, I<@callback>) | ||
| 504 | |||
| 505 | unhooks a hook from before stack of the callback associated with 'command_name'. | ||
| 506 | see L<Tk::AppWindow::BaseClasses::Callback>. | ||
| 507 | |||
| 508 | =cut | ||
| 509 | |||
| 510 | sub cmdUnhookBefore { | ||
| 511 | my $self = shift; | ||
| 512 | return $self->CmdHook('unhookBefore', @_); | ||
| 513 | } | ||
| 514 | |||
| 515 | =item B<configGet>I<('-option')> | ||
| 516 | |||
| 517 | Equivalent to $app-cget. Except here you can also specify | ||
| 518 | the options added by B<configInit> | ||
| 519 | |||
| 520 | =cut | ||
| 521 | |||
| 522 | sub configGet { | ||
| 523 | my ($self, $option) = @_; | ||
| 524 | croak "Option not defined" unless defined $option; | ||
| 525 | if (exists $self->{CONFIGTABLE}->{$option}) { | ||
| 526 | my $call = $self->{CONFIGTABLE}->{$option}; | ||
| 527 | return $call->execute; | ||
| 528 | } else { | ||
| 529 | return $self->cget($option); | ||
| 530 | } | ||
| 531 | } | ||
| 532 | |||
| 533 | sub ConfigHook { | ||
| 534 | my $self = shift; | ||
| 535 | my $method = shift; | ||
| 536 | my $config = shift; | ||
| 537 | if (exists $self->{CONFIGTABLE}->{$config}) { | ||
| 538 | my $call = $self->{CONFIGTABLE}->{$config}; | ||
| 539 | $call->$method(@_); | ||
| 540 | return 1 | ||
| 541 | } | ||
| 542 | carp "Config option '$config' was not defined through configInit"; | ||
| 543 | return 0 | ||
| 544 | } | ||
| 545 | |||
| 546 | =item B<configHookAfter>(I<'-configvariable'>, I<@callback>) | ||
| 547 | |||
| 548 | Adds a hook to the after stack of the callback associated with a config | ||
| 549 | variable'. See L<Tk::AppWindow::BaseClasses::Callback>. | ||
| 550 | Only works on config variables created through B<configInit> | ||
| 551 | |||
| 552 | =cut | ||
| 553 | |||
| 554 | sub configHookAfter { | ||
| 555 | my $self = shift; | ||
| 556 | return $self->ConfigHook('hookAfter', @_); | ||
| 557 | } | ||
| 558 | |||
| 559 | =item B<configHookAfter>(I<'-configvariable'>, I<@callback>) | ||
| 560 | |||
| 561 | Adds a hook to teh before stack of the callback associated with a config | ||
| 562 | variable'. See L<Tk::AppWindow::BaseClasses::Callback>. | ||
| 563 | Only works on config variables created through B<configInit> | ||
| 564 | |||
| 565 | =cut | ||
| 566 | |||
| 567 | sub configHookBefore { | ||
| 568 | my $self = shift; | ||
| 569 | return $self->ConfigHook('hookBefore', @_); | ||
| 570 | } | ||
| 571 | |||
| 572 | =item B<configInit>I<(@options)> | ||
| 573 | |||
| 574 | $app->configInit( | ||
| 575 | -option1 => ['method', $obj, @options, $default], | ||
| 576 | -option2 => [sub { do something }, @options, $default], | ||
| 577 | ); | ||
| 578 | |||
| 579 | Add options to the options table. Usually called at create time. But worth experimenting with. | ||
| 580 | |||
| 581 | Always specify I<$default>. The last thing you specify is always the default value. set it undef | ||
| 582 | if you want it to be ignored. | ||
| 583 | |||
| 584 | =cut | ||
| 585 | |||
| 586 | sub configInit { | ||
| 587 | my $self = shift; | ||
| 588 | my $args = $self->{ARGS}; | ||
| 589 | my $table = $self->{CONFIGTABLE}; | ||
| 590 | while (@_) { | ||
| 591 | my $option = shift; | ||
| 592 | my $i = shift; | ||
| 593 | my ($call, $owner, @options) = @$i; | ||
| 594 | my $default = pop @options; | ||
| 595 | my $value; | ||
| 596 | $value = delete $args->{$option} if defined $args; | ||
| 597 | $value = $default unless defined $value; | ||
| 598 | unless (exists $table->{$option}) { | ||
| 599 | $table->{$option} = $self->CreateCallback($call, $owner, @options); | ||
| 600 | $self->configPut($option, $value) if defined $value; | ||
| 601 | } else { | ||
| 602 | croak "Config option $option already defined\n"; | ||
| 603 | } | ||
| 604 | } | ||
| 605 | } | ||
| 606 | |||
| 607 | =item B<configMode> | ||
| 608 | |||
| 609 | Returns 1 if MainLoop is not yet running. | ||
| 610 | |||
| 611 | =cut | ||
| 612 | |||
| 613 | sub configMode { | ||
| 614 | return exists $_[0]->{ARGS}; | ||
| 615 | } | ||
| 616 | |||
| 617 | =item B<configPut>I<(-option => $value)> | ||
| 618 | |||
| 619 | Equivalent to $app-configure. Except here you can also specify | ||
| 620 | the options added by B<configInit> | ||
| 621 | |||
| 622 | =cut | ||
| 623 | |||
| 624 | sub configPut { | ||
| 625 | my ($self, $option, $value) = @_; | ||
| 626 | croak "Option not defined" unless defined $option; | ||
| 627 | if (exists $self->{CONFIGTABLE}->{$option}) { | ||
| 628 | my $call = $self->{CONFIGTABLE}->{$option}; | ||
| 629 | $call->execute($value); | ||
| 630 | } else { | ||
| 631 | $self->configure($option, $value); | ||
| 632 | } | ||
| 633 | } | ||
| 634 | |||
| 635 | =item B<configRemove>I<('-option')> | ||
| 636 | |||
| 637 | Removes '-option from the config hash. Usefull when unloading a plugin | ||
| 638 | that configures an option with configInit. | ||
| 639 | |||
| 640 | =cut | ||
| 641 | |||
| 642 | sub configRemove { | ||
| 643 | my ($self, $option) = @_; | ||
| 644 | delete $self->{CONFIGTABLE}->{$option}; | ||
| 645 | } | ||
| 646 | |||
| 647 | =item B<configUnhookAfter>(I<'-configvariable'>, I<@callback>) | ||
| 648 | |||
| 649 | Removes a hook from the after stack of the callback associated with a config | ||
| 650 | variable'. See L<Tk::AppWindow::BaseClasses::Callback>. | ||
| 651 | Only works on config variables created through B<configInit> | ||
| 652 | |||
| 653 | =cut | ||
| 654 | |||
| 655 | sub configUnhookAfter { | ||
| 656 | my $self = shift; | ||
| 657 | return $self->CmdHook('unhookAfter', @_); | ||
| 658 | } | ||
| 659 | |||
| 660 | =item B<configUnhookBefore>(I<'-configvariable'>, I<@callback>) | ||
| 661 | |||
| 662 | Removes a hook from the after stack of the callback associated with a config | ||
| 663 | variable'. See L<Tk::AppWindow::BaseClasses::Callback>. | ||
| 664 | Only works on config variables created through B<configInit> | ||
| 665 | |||
| 666 | =cut | ||
| 667 | |||
| 668 | sub configUnhookBefore { | ||
| 669 | my $self = shift; | ||
| 670 | return $self->CmdHook('unhookBefore', @_); | ||
| 671 | } | ||
| 672 | |||
| 673 | =item B<CreateCallback>('MethodName', $owner, @options); | ||
| 674 | |||
| 675 | =item B<CreateCallback>(sub { do whatever }, @options); | ||
| 676 | |||
| 677 | Creates and returns a Tk::AppWindow::Baseclasses::Callback object. | ||
| 678 | A convenience method that saves you some typing. | ||
| 679 | |||
| 680 | =cut | ||
| 681 | |||
| 682 | sub CreateCallback { | ||
| 683 | my $self = shift; | ||
| 684 | return Tk::AppWindow::BaseClasses::Callback->new(@_); | ||
| 685 | } | ||
| 686 | |||
| 687 | =item B<extExists>I<($name)> | ||
| 688 | |||
| 689 | Returns 1 if $name is loaded. | ||
| 690 | |||
| 691 | =cut | ||
| 692 | |||
| 693 | sub extExists { | ||
| 694 | my ($self, $name) = @_; | ||
| 695 | my $plgs = $self->{EXTENSIONS}; | ||
| 696 | return exists $plgs->{$name}; | ||
| 697 | } | ||
| 698 | |||
| 699 | =item B<extGet>('Name') | ||
| 700 | |||
| 701 | Returns reference to extension object 'Name'. | ||
| 702 | Returns undef if 'Name' is not loaded. | ||
| 703 | |||
| 704 | =cut | ||
| 705 | |||
| 706 | sub extGet { | ||
| 707 | my ($self, $name) = @_; | ||
| 708 | my $plgs = $self->{EXTENSIONS}; | ||
| 709 | if (exists $plgs->{$name}) { | ||
| 710 | return $plgs->{$name} | ||
| 711 | } | ||
| 712 | return undef | ||
| 713 | } | ||
| 714 | |||
| 715 | =item B<extList> | ||
| 716 | |||
| 717 | Returns a list of all loaded extensions | ||
| 718 | |||
| 719 | =cut | ||
| 720 | |||
| 721 | sub extList { | ||
| 722 | my $self = shift; | ||
| 723 | my $pl = $self->{EXTLOADORDER}; | ||
| 724 | return @$pl; | ||
| 725 | } | ||
| 726 | |||
| 727 | =item B<extLoad>('Name'); | ||
| 728 | |||
| 729 | Loads and initializes an extension. | ||
| 730 | Terminates application if it fails. | ||
| 731 | |||
| 732 | Called at create time. | ||
| 733 | |||
| 734 | =cut | ||
| 735 | |||
| 736 | sub extLoad { | ||
| 737 | my ($self, $name) = @_; | ||
| 738 | my $exts = $self->{EXTENSIONS}; | ||
| 739 | my $ext = undef; | ||
| 740 | unless (exists $exts->{$name}) { #unless already loaded | ||
| 741 | my @paths = ('Tk::AppWindow::Ext'); | ||
| 742 | my $namespace = $self->NameSpace; | ||
| 743 | if (defined $namespace) { | ||
| 744 | $namespace = $namespace . '::Ext'; | ||
| 745 | push @paths, $namespace; | ||
| 746 | } | ||
| 747 | my $error = ''; | ||
| 748 | for (@paths) { | ||
| 749 | my $p = $_; | ||
| 750 | |||
| 751 | my $modname = $p . "::$name"; | ||
| 752 | eval "use $modname;"; | ||
| 753 | $error = $@; | ||
| 754 | unless ($error) { | ||
| 755 | my $ext = $modname->new($self); | ||
| 756 | $exts->{$name} = $ext; | ||
| 757 | my $o = $self->{EXTLOADORDER}; | ||
| 758 | push @$o, $name; | ||
| 759 | return | ||
| 760 | } | ||
| 761 | } | ||
| 762 | die "unable to load extension $name\n$error"; | ||
| 763 | } | ||
| 764 | } | ||
| 765 | |||
| 766 | =item B<fileSeparator> | ||
| 767 | |||
| 768 | Returns the correct file separator for your operating system. | ||
| 769 | '\' for windows and '/' for all the others. | ||
| 770 | |||
| 771 | =cut | ||
| 772 | |||
| 773 | sub fileSeparator { | ||
| 774 | my $self = shift; | ||
| 775 | return '\\' if $mswin; | ||
| 776 | return '/' | ||
| 777 | } | ||
| 778 | |||
| 779 | =item B<fontFams> | ||
| 780 | |||
| 781 | Replaces the method I<fontFamilies>, which crashes when used under windows. | ||
| 782 | This one queries the registry for font families on Windows, and calls | ||
| 783 | I<fontFamilies> on other operating systems. | ||
| 784 | |||
| 785 | =cut | ||
| 786 | |||
| 787 | sub fontFams { | ||
| 788 | my $self = shift; | ||
| 789 | if ($mswin) { | ||
| 790 | my @fams; | ||
| 791 | my $raw = `reg query "HKLM\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Fonts" /s`; | ||
| 792 | my @lines = split /\n/, $raw; | ||
| 793 | for (@lines) { | ||
| 794 | my $lin = $_; | ||
| 795 | chomp $lin; | ||
| 796 | next if $lin eq ''; | ||
| 797 | next if $lin =~ /^HKEY/; | ||
| 798 | $lin =~ s/^\s+//; #remove leading spaces | ||
| 799 | $lin =~ s/^([^\(]+).*/$1/; #remove all as of TrueType; | ||
| 800 | $lin =~ s/\s+$//; #remove trailing spaces | ||
| 801 | next if $lin =~ /bold/i; #ignore bold | ||
| 802 | next if $lin =~ /italic/i; #ignore italic | ||
| 803 | next if $lin =~ /\.FON$/; #only truetype fonts | ||
| 804 | $lin =~ s/\s\d+.+$//; #adjust for some older fonts | ||
| 805 | push @fams, $lin; | ||
| 806 | } | ||
| 807 | return @fams | ||
| 808 | } else { | ||
| 809 | return $self->fontFamilies; | ||
| 810 | } | ||
| 811 | } | ||
| 812 | |||
| 813 | sub GetArgsRef { return $_[0]->{ARGS} } | ||
| 814 | |||
| 815 | =item B<getArt>I<($icon, $size)> | ||
| 816 | |||
| 817 | Checks if extension B<Art> is loaded and returns requested image if so. | ||
| 818 | If $size is not specified, default size is used. | ||
| 819 | |||
| 820 | =cut | ||
| 821 | |||
| 822 | sub getArt { | ||
| 823 | my ($self, $icon, $size) = @_; | ||
| 824 | my $art = $self->extGet('Art'); | ||
| 825 | if (defined $art) { | ||
| 826 | return $art->getIcon($icon, $size); | ||
| 827 | } | ||
| 828 | return undef | ||
| 829 | } | ||
| 830 | |||
| 831 | sub getDirIcon { | ||
| 832 | my ($self, $name, $mode) = @_; | ||
| 833 | my $opstring = '-' . $mode . 'viewsize'; | ||
| 834 | my $size = $self->cget('-' . $mode . 'viewsize'); | ||
| 835 | my $icon = $self->getArt('folder', $size); | ||
| 836 | return $icon if defined $icon; | ||
| 837 | return $self->Subwidget('FilePicker')->Subwidget('Browser')->DefaultDirIcon; | ||
| 838 | } | ||
| 839 | |||
| 840 | sub getFileIcon { | ||
| 841 | my ($self, $name, $mode) = @_; | ||
| 842 | my $size = $self->cget('-' . $mode . 'viewsize'); | ||
| 843 | my $art = $self->extGet('Art'); | ||
| 844 | my $icon = $art->getFileIcon($name, $size) if defined $art; | ||
| 845 | return $icon if defined $icon; | ||
| 846 | return $self->Subwidget('FilePicker')->Subwidget('Browser')->DefaultFileIcon($name, $mode); | ||
| 847 | } | ||
| 848 | |||
| 849 | sub getLinkIcon { | ||
| 850 | my ($self, $name, $mode) = @_; | ||
| 851 | my $size = $self->cget('-' . $mode . 'viewsize'); | ||
| 852 | my $icon = $self->getArt('emblem-symbolic-link', $size); | ||
| 853 | return $icon if defined $icon; | ||
| 854 | return $self->Subwidget('FilePicker')->Subwidget('Browser')->DefaultLinkIcon($name, $mode); | ||
| 855 | } | ||
| 856 | |||
| 857 | =item B<log>I<($message)> | ||
| 858 | |||
| 859 | Log $message | ||
| 860 | |||
| 861 | =cut | ||
| 862 | |||
| 863 | sub log { | ||
| 864 | my ($self, $message) = @_; | ||
| 865 | return if $self->logDisabled; | ||
| 866 | $self->Callback('-logcall', $message); | ||
| 867 | } | ||
| 868 | |||
| 869 | =item B<logDisabled>I<(?$flag?)> | ||
| 870 | |||
| 871 | Disable/enable logging of messages. | ||
| 872 | |||
| 873 | =cut | ||
| 874 | |||
| 875 | sub logDisabled { | ||
| 876 | my $self = shift; | ||
| 877 | $self->{LOGDISABLED} = shift if @_; | ||
| 878 | return $self->{LOGDISABLED} | ||
| 879 | } | ||
| 880 | |||
| 881 | =item B<logError>I<($message)> | ||
| 882 | |||
| 883 | Log $message as an error | ||
| 884 | |||
| 885 | =cut | ||
| 886 | |||
| 887 | sub logError { | ||
| 888 | my ($self, $message) = @_; | ||
| 889 | return if $self->logDisabled; | ||
| 890 | $self->Callback('-logerrorcall', $message); | ||
| 891 | } | ||
| 892 | |||
| 893 | =item B<logWarning>I<($message)> | ||
| 894 | |||
| 895 | Log $message as a warning | ||
| 896 | |||
| 897 | =cut | ||
| 898 | |||
| 899 | sub logWarning { | ||
| 900 | my ($self, $message) = @_; | ||
| 901 | return if $self->logDisabled; | ||
| 902 | $self->Callback('-logwarningcall', $message); | ||
| 903 | } | ||
| 904 | |||
| 905 | =item B<MenuItems> | ||
| 906 | |||
| 907 | Returns a list of two items used by the B<MenuBar> extension. The first defines the application menu. | ||
| 908 | The second is the menu option Quit in this menu. Overwrite this method to make it return | ||
| 909 | a different list. See also L<Tk::AppWindow::Ext::MenuBar> | ||
| 910 | |||
| 911 | =cut | ||
| 912 | |||
| 913 | sub MenuItems { | ||
| 914 | my $self = shift; | ||
| 915 | return ( | ||
| 916 | #This table is best viewed with tabsize 3. | ||
| 917 | # type menupath label cmd icon keyb config variable | ||
| 918 | [ 'menu', undef, "~appname", ], | ||
| 919 | [ 'menu_normal', 'appname::', "~Quit", 'quit', 'application-exit', 'CTRL+Q', ], | ||
| 920 | ) | ||
| 921 | } | ||
| 922 | |||
| 923 | sub NameSpace { | ||
| 924 | return $_[0]->{NAMESPACE} | ||
| 925 | } | ||
| 926 | |||
| 927 | =item B<openURL>I<($file_or_web)> | ||
| 928 | |||
| 929 | Opens I<$file_or_web> in the default application of your desktop. | ||
| 930 | |||
| 931 | Please provide 'https://' or whatever protocol in front if it is on the web. | ||
| 932 | |||
| 933 | =cut | ||
| 934 | |||
| 935 | sub openURL { | ||
| 936 | my ($self, $url) = @_; | ||
| 937 | # print "is web $url\n" if $url =~ /^[A-Za-z]+:\/\//; | ||
| 938 | if ($mswin) { | ||
| 939 | if ($url =~ /^[A-Za-z]+:\/\//) { #is a web document | ||
| 940 | system("explorer \"$url\""); | ||
| 941 | } else { | ||
| 942 | system("\"$url\""); | ||
| 943 | } | ||
| 944 | } else { | ||
| 945 | system("xdg-open \"$url\""); | ||
| 946 | } | ||
| 947 | } | ||
| 948 | |||
| 949 | =item B<pause>I<($time)> | ||
| 950 | |||
| 951 | Pauses the app for $time miliseconds in a non blocking way. | ||
| 952 | |||
| 953 | =cut | ||
| 954 | |||
| 955 | sub pause { | ||
| 956 | my ($self, $time) = @_; | ||
| 957 | my $var = 0; | ||
| 958 | $self->after($time, sub { $var = 1 }); | ||
| 959 | $self->waitVariable(\$var); | ||
| 960 | } | ||
| 961 | |||
| 962 | sub pick { | ||
| 963 | my $self = shift; | ||
| 964 | my $picker = $self->pickGet; | ||
| 965 | return $picker->pick(@_); | ||
| 966 | } | ||
| 967 | |||
| 968 | sub pickGet { | ||
| 969 | my $self = shift; | ||
| 970 | my $picker = $self->Subwidget('FilePicker'); | ||
| 971 | unless (defined $picker) { | ||
| 972 | #create file picker widget | ||
| 973 | $picker = $self->FilePicker( | ||
| 974 | -diriconcall => ['getDirIcon', $self], | ||
| 975 | -fileiconcall => ['getFileIcon', $self], | ||
| 976 | -linkiconcall => ['getLinkIcon', $self], | ||
| 977 | ); | ||
| 978 | $self->Advertise('FilePicker', $picker); | ||
| 979 | $picker->geometry('640x400+100+100'); | ||
| 980 | |||
| 981 | #create filepicker images | ||
| 982 | my @images = ( | ||
| 983 | ['-compactimage', 'view-list-details', 16], | ||
| 984 | ['-detailsimage', 'view-list-tree', 16], | ||
| 985 | ['-iconviewimage', 'view-list-icons', 16], | ||
| 986 | ['-msgimage', 'dialog-information', 32], | ||
| 987 | ['-newfolderimage', 'folder-new', 16], | ||
| 988 | ['-reloadimage', 'appointment-recurring', 16], | ||
| 989 | ['-warnimage', 'dialog-warning', 32], | ||
| 990 | ); | ||
| 991 | for (@images) { | ||
| 992 | my ($opt, $icon, $size) = @$_; | ||
| 993 | my $img = $self->getArt($icon, $size); | ||
| 994 | $picker->configure($opt, $img) if defined $img; | ||
| 995 | } | ||
| 996 | } | ||
| 997 | return $picker | ||
| 998 | } | ||
| 999 | |||
| 1000 | sub pickFileOpen { | ||
| 1001 | my $self = shift; | ||
| 1002 | my $picker = $self->pickGet; | ||
| 1003 | return $picker->pickFileOpen(@_); | ||
| 1004 | } | ||
| 1005 | |||
| 1006 | sub pickFileOpenMulti { | ||
| 1007 | my $self = shift; | ||
| 1008 | my $picker = $self->pickGet; | ||
| 1009 | return $picker->pickFileOpenMulti(@_); | ||
| 1010 | } | ||
| 1011 | |||
| 1012 | sub pickFileSave { | ||
| 1013 | my $self = shift; | ||
| 1014 | my $picker = $self->pickGet; | ||
| 1015 | return $picker->pickFileSave(@_); | ||
| 1016 | } | ||
| 1017 | |||
| 1018 | sub pickFolderSelect { | ||
| 1019 | my $self = shift; | ||
| 1020 | my $picker = $self->pickGet; | ||
| 1021 | return $picker->pickFolderSelect(@_); | ||
| 1022 | } | ||
| 1023 | |||
| 1024 | =item B<popDialog>I<($title, $message, $icon, @buttons)> | ||
| 1025 | |||
| 1026 | Pops up a dialogbox with @buttons. | ||
| 1027 | The first button is the default button. | ||
| 1028 | Returns the name of the button pressed. | ||
| 1029 | If you press the Escape key it wil return '*Cancel*'. | ||
| 1030 | |||
| 1031 | =cut | ||
| 1032 | |||
| 1033 | sub popDialog { | ||
| 1034 | my ($self, $title, $text, $icon, @buttons) = @_; | ||
| 1035 | $icon = 'dialog-question' unless defined $icon; | ||
| 1036 | my @padding = (-padx => 10, -pady => 10); | ||
| 1037 | my $q = $self->YADialog( | ||
| 1038 | -title => $title, | ||
| 1039 | -buttons => \@buttons, | ||
| 1040 | -defaultbutton => $buttons[0], | ||
| 1041 | ); | ||
| 1042 | my $img = $self->getArt($icon, 48); | ||
| 1043 | $q->Label(-image => $img)->pack(-side => 'left', @padding) if defined $img; | ||
| 1044 | $q->Label( | ||
| 1045 | -anchor => 'w', | ||
| 1046 | -text => $text, | ||
| 1047 | )->pack(-side => 'left', -fill => 'x', @padding); | ||
| 1048 | my $answer = $q->Show(-popover => $self); | ||
| 1049 | $q->destroy; | ||
| 1050 | return $answer | ||
| 1051 | } | ||
| 1052 | |||
| 1053 | =item B<popEntry>I<($title, $message, $value, $icon)> | ||
| 1054 | |||
| 1055 | Pops up a dialog box with an Entry widget. | ||
| 1056 | returns the entered value if the ok button is pressed. | ||
| 1057 | Otherwise returns undef.. | ||
| 1058 | |||
| 1059 | =cut | ||
| 1060 | |||
| 1061 | sub popEntry { | ||
| 1062 | my ($self, $title, $text, $value, $icon) = @_; | ||
| 1063 | $icon = 'dialog-information' unless defined $icon; | ||
| 1064 | my @padding = (-padx => 10, -pady => 10); | ||
| 1065 | my $q = $self->YADialog( | ||
| 1066 | -title => $title, | ||
| 1067 | -buttons => [qw(Ok Cancel)], | ||
| 1068 | ); | ||
| 1069 | $q->Label(-image => $self->getArt($icon, 48))->pack(-side => 'left', @padding); | ||
| 1070 | my $f = $q->Frame->pack(-side => 'left', @padding); | ||
| 1071 | $f->Label( | ||
| 1072 | -anchor => 'w', | ||
| 1073 | -text => $text, | ||
| 1074 | )->pack(-fill => 'x', -padx => 2, -pady => 2); | ||
| 1075 | my $e = $f->Entry->pack(-fill => 'x', -padx => 2, -pady => 2); | ||
| 1076 | $e->insert('end', $value) if defined $value; | ||
| 1077 | $e->focus; | ||
| 1078 | $e->bind('<Return>', sub { | ||
| 1079 | $q->{PRESSED} = 'Ok' | ||
| 1080 | }); | ||
| 1081 | |||
| 1082 | my $result; | ||
| 1083 | my $answer = $q->Show(-popover => $self); | ||
| 1084 | $result = $e->get if $answer eq 'Ok'; | ||
| 1085 | |||
| 1086 | $q->destroy; | ||
| 1087 | return $result | ||
| 1088 | } | ||
| 1089 | |||
| 1090 | =item B<popForm>I<(%options)> | ||
| 1091 | |||
| 1092 | This method uses L<Tk::QuickForm> to pop up a form for the user to fill out. | ||
| 1093 | Returns a hash with all defined keys if the user clicks the Ok button, | ||
| 1094 | Returns an empty hash if the user clicks cancel. | ||
| 1095 | It can take the following options: | ||
| 1096 | |||
| 1097 | =over 4 | ||
| 1098 | |||
| 1099 | =item B<-acceptempty> | ||
| 1100 | |||
| 1101 | Boolean. By default false. If set, empty fields will not evaluate as error. | ||
| 1102 | |||
| 1103 | =item B<-initialvalues> | ||
| 1104 | |||
| 1105 | A reference to a hash with default values to be loaded. | ||
| 1106 | |||
| 1107 | =item B<-oktext> | ||
| 1108 | |||
| 1109 | By default 'Ok'. You can change it to any text you like. | ||
| 1110 | |||
| 1111 | =item B<-structure> | ||
| 1112 | |||
| 1113 | Same option as in L<Tk::QuickForm>. See there. | ||
| 1114 | |||
| 1115 | =item B<-title> | ||
| 1116 | |||
| 1117 | The window title. | ||
| 1118 | |||
| 1119 | =back | ||
| 1120 | |||
| 1121 | =cut | ||
| 1122 | |||
| 1123 | sub popForm { | ||
| 1124 | my ($self, %args) = @_; | ||
| 1125 | |||
| 1126 | my $structure = delete $args{'-structure'}; | ||
| 1127 | croak "Option -structure not set" unless defined $structure; | ||
| 1128 | |||
| 1129 | my $initial = delete $args{'-initialvalues'}; | ||
| 1130 | |||
| 1131 | my $oktext = delete $args{'-oktext'}; | ||
| 1132 | $oktext = 'Ok' unless defined $oktext; | ||
| 1133 | |||
| 1134 | my $acceptempty = delete $args{'-acceptempty'}; | ||
| 1135 | $acceptempty = 0 unless defined $acceptempty; | ||
| 1136 | |||
| 1137 | my $q = $self->YADialog(%args, | ||
| 1138 | -buttons => ['Cancel'], | ||
| 1139 | ); | ||
| 1140 | my $b = $q->Subwidget('buttonframe')->Button( | ||
| 1141 | -text => $oktext, | ||
| 1142 | -command => sub { | ||
| 1143 | $q->Pressed('Ok') | ||
| 1144 | } | ||
| 1145 | ); | ||
| 1146 | $q->ButtonPack($b); | ||
| 1147 | |||
| 1148 | #getting icons for Tk::QuickForm | ||
| 1149 | my %qopts = (); | ||
| 1150 | my @images = ( | ||
| 1151 | ['-fileimage', 'text-x-plain', 16], | ||
| 1152 | ['-folderimage', 'folder', 16], | ||
| 1153 | ['-fontimage', 'gtk-select-font', 16], | ||
| 1154 | ['-msgimage', 'dialog-question', 32], | ||
| 1155 | ['-newfolderimage', 'folder-new', 16], | ||
| 1156 | ['-reloadimage', 'appointment-recurring', 16], | ||
| 1157 | ['-warnimage', 'dialog-warning', 32], | ||
| 1158 | ); | ||
| 1159 | for (@images) { | ||
| 1160 | my ($opt, $icon, $size) = @$_; | ||
| 1161 | my $img = $self->getArt($icon, $size); | ||
| 1162 | $qopts{$opt} = $img if defined $img; | ||
| 1163 | } | ||
| 1164 | |||
| 1165 | my $form = $q->QuickForm(%qopts, | ||
| 1166 | -acceptempty => $acceptempty, | ||
| 1167 | -diriconcall => ['getDirIcon', $self], | ||
| 1168 | -fileiconcall => ['getFileIcon', $self], | ||
| 1169 | -linkiconcall => ['getLinkIcon', $self], | ||
| 1170 | -structure => $structure, | ||
| 1171 | -postvalidatecall => sub { | ||
| 1172 | my $flag = shift; | ||
| 1173 | if ($flag) { | ||
| 1174 | $b->configure('-state', 'normal') | ||
| 1175 | } else { | ||
| 1176 | $b->configure('-state', 'disabled') | ||
| 1177 | } | ||
| 1178 | }, | ||
| 1179 | )->pack(-expand => 1, -fill => 'both', -padx => 2, -pady => 2); | ||
| 1180 | $form->createForm; | ||
| 1181 | $form->put(%$initial) if defined $initial; | ||
| 1182 | |||
| 1183 | my %result = (); | ||
| 1184 | my $answer = $q->Show(-popover => $self); | ||
| 1185 | %result = $form->get if $answer eq 'Ok'; | ||
| 1186 | |||
| 1187 | $q->destroy; | ||
| 1188 | return %result | ||
| 1189 | } | ||
| 1190 | |||
| 1191 | =item B<popMessage>I<($message, $icon, ?$size?)> | ||
| 1192 | |||
| 1193 | Pops up a message box with a close button. | ||
| 1194 | |||
| 1195 | =cut | ||
| 1196 | |||
| 1197 | sub popMessage { | ||
| 1198 | my ($self, $text, $icon, $size) = @_; | ||
| 1199 | $icon = 'dialog-information' unless defined $icon; | ||
| 1200 | $size = 48 unless defined $size; | ||
| 1201 | my $m = $self->YAMessage( | ||
| 1202 | -title => 'Message', | ||
| 1203 | -text => $text, | ||
| 1204 | -image => $self->getArt($icon, $size), | ||
| 1205 | ); | ||
| 1206 | $m->Show(-popover => $self); | ||
| 1207 | $m->destroy; | ||
| 1208 | } | ||
| 1209 | |||
| 1210 | sub popTest { | ||
| 1211 | my $self = shift; | ||
| 1212 | $self->popMessage('You did something'); | ||
| 1213 | } | ||
| 1214 | |||
| 1215 | sub PostConfig { | ||
| 1216 | my $self = shift; | ||
| 1217 | delete $self->{ARGS}; | ||
| 1218 | |||
| 1219 | #set logo | ||
| 1220 | my $lgf = $self->cget('-logo'); | ||
| 1221 | if ((defined $lgf) and (-e $lgf)) { | ||
| 1222 | my $logo = $self->Photo(-file => $lgf, -format => 'PNG'); | ||
| 1223 | $self->iconimage($logo); | ||
| 1224 | } | ||
| 1225 | #set geometry | ||
| 1226 | if ($self->configGet('-savegeometry')) { | ||
| 1227 | my $cf = $self->extGet('ConfigFolder'); | ||
| 1228 | if (defined $cf) { | ||
| 1229 | my ($g) = $cf->loadList('geometry', 'aw geometry'); | ||
| 1230 | $self->geometry($g) if defined $g; | ||
| 1231 | } | ||
| 1232 | } | ||
| 1233 | my $pc = $self->{POSTCONFIG}; | ||
| 1234 | for (@$pc) { | ||
| 1235 | # $self->pause(100); | ||
| 1236 | $_->execute | ||
| 1237 | } | ||
| 1238 | |||
| 1239 | |||
| 1240 | } | ||
| 1241 | |||
| 1242 | =item B<StatusAttach>I<$widget, $text> | ||
| 1243 | |||
| 1244 | Attaches a status message to $widget | ||
| 1245 | |||
| 1246 | =cut | ||
| 1247 | |||
| 1248 | sub StatusAttach { | ||
| 1249 | my ($self, $widget, $text) = @_; | ||
| 1250 | my $s = $self->extGet('StatusBar'); | ||
| 1251 | $s->Attach($widget, $text) if defined $s; | ||
| 1252 | } | ||
| 1253 | |||
| 1254 | =item B<StatusMessage>I<($text>)> | ||
| 1255 | |||
| 1256 | Sends a message to the status bar if it is loaded. See L<Tk::AppWindow::Ext::StatusBar> | ||
| 1257 | |||
| 1258 | =cut | ||
| 1259 | |||
| 1260 | sub StatusMessage { | ||
| 1261 | my $self = shift; | ||
| 1262 | my $sb = $self->extGet('StatusBar'); | ||
| 1263 | $sb->Message(@_) if defined $sb; | ||
| 1264 | } | ||
| 1265 | |||
| 1266 | =item B<progressAdd>I<($name, $label, $size, $variable)> | ||
| 1267 | |||
| 1268 | Adds a progress bar to the status bar. | ||
| 1269 | Extension B<StatusBar> must be loaded for this to work. | ||
| 1270 | |||
| 1271 | =cut | ||
| 1272 | |||
| 1273 | sub progressAdd { | ||
| 1274 | my ($self, $name, $label, $size, $variable) = @_; | ||
| 1275 | my $sb = $self->extGet('StatusBar'); | ||
| 1276 | $sb->AddProgressItem($name, | ||
| 1277 | -label => $label, | ||
| 1278 | -length => 150, | ||
| 1279 | -from => 0, | ||
| 1280 | -to => $size, | ||
| 1281 | -variable => $variable, | ||
| 1282 | ) if defined $sb; | ||
| 1283 | } | ||
| 1284 | |||
| 1285 | =item B<progressRemove>I<($name)> | ||
| 1286 | |||
| 1287 | Removes a progress bar from the status bar. | ||
| 1288 | Extension B<StatusBar> must be loaded for this to work. | ||
| 1289 | |||
| 1290 | =cut | ||
| 1291 | |||
| 1292 | sub progressRemove { | ||
| 1293 | my ($self, $name) = @_; | ||
| 1294 | my $sb = $self->extGet('StatusBar'); | ||
| 1295 | $sb->Delete($name) if defined $sb; | ||
| 1296 | } | ||
| 1297 | |||
| 1298 | =item B<ToolItems> | ||
| 1299 | |||
| 1300 | Returns an empty list. It is called by the B<ToolBar> extension. Overwrite it | ||
| 1301 | if you like. | ||
| 1302 | |||
| 1303 | =cut | ||
| 1304 | |||
| 1305 | sub ToolItems { | ||
| 1306 | my $self = shift; | ||
| 1307 | return ( | ||
| 1308 | ) | ||
| 1309 | } | ||
| 1310 | |||
| 1311 | sub WorkSpace { | ||
| 1312 | my $self = shift; | ||
| 1313 | $self->{WORKSPACE} = $self->Subwidget(shift) if @_; | ||
| 1314 | return $self->{WORKSPACE} | ||
| 1315 | } | ||
| 1316 | |||
| 1317 | =back | ||
| 1318 | |||
| 1319 | =head1 FILE DIALOGS | ||
| 1320 | |||
| 1321 | The B<pickWhatEver> methods launch filedialogs. They are meant as a replacement | ||
| 1322 | of the classic B<getOpenFile> and B<getSaveFile> methods. The pick methods | ||
| 1323 | can be called with a lot of the options for L<Tk::FilePicker> and L<Tk::FileBrowser>. | ||
| 1324 | See also there. Additionaly, they can also be called with these options: | ||
| 1325 | |||
| 1326 | =over 4 | ||
| 1327 | |||
| 1328 | =item B<-initialdir> | ||
| 1329 | |||
| 1330 | Directory to load on pop. | ||
| 1331 | |||
| 1332 | =item B<-initialfile> | ||
| 1333 | |||
| 1334 | Suggested file name. | ||
| 1335 | |||
| 1336 | =back | ||
| 1337 | |||
| 1338 | The pick methods always return their results in list context. So | ||
| 1339 | even when you expect only one result you have to do: | ||
| 1340 | |||
| 1341 | my ($file) = $fp->pickWhatEver(%options); | ||
| 1342 | |||
| 1343 | =over 4 | ||
| 1344 | |||
| 1345 | =item B<pick> | ||
| 1346 | |||
| 1347 | The basic pick method. | ||
| 1348 | |||
| 1349 | =item B<pickFileOpen> | ||
| 1350 | |||
| 1351 | Calls B<pick> configured to select one file for opening. Equivalent to: | ||
| 1352 | |||
| 1353 | my ($file) = $window->pick( | ||
| 1354 | -checkoverwrite => 0, | ||
| 1355 | -showfolders => 1, | ||
| 1356 | -showfiles => 1, | ||
| 1357 | -selectmode => 'single', | ||
| 1358 | -selectstring => 'Open', | ||
| 1359 | -title => 'Open file', | ||
| 1360 | ); | ||
| 1361 | |||
| 1362 | =item B<pickFileOpenMulti> | ||
| 1363 | |||
| 1364 | Calls B<pick> configured to select multiple files for opening. Equivalent to: | ||
| 1365 | |||
| 1366 | my @files = $window->pick( | ||
| 1367 | -checkoverwrite => 0, | ||
| 1368 | -showfolders => 1, | ||
| 1369 | -showfiles => 1, | ||
| 1370 | -selectmode => 'extended', | ||
| 1371 | -selectstring => 'Open', | ||
| 1372 | -title => 'Open file', | ||
| 1373 | ); | ||
| 1374 | |||
| 1375 | =item B<pickFileSave> | ||
| 1376 | |||
| 1377 | Calls B<pick> configured to select one file for saving. Pops | ||
| 1378 | a dialog for overwrite if the selected file exists. Equivalent to: | ||
| 1379 | |||
| 1380 | my ($file) = $window->pick( | ||
| 1381 | -checkoverwrite => 1, | ||
| 1382 | -showfolders => 1, | ||
| 1383 | -showfiles => 1, | ||
| 1384 | -selectmode => 'single', | ||
| 1385 | -selectstring => 'Save', | ||
| 1386 | -title => 'Save file', | ||
| 1387 | ); | ||
| 1388 | |||
| 1389 | =item B<pickFolderSelect> | ||
| 1390 | |||
| 1391 | Calls B<pick> configured to select one folder. Equivalent to: | ||
| 1392 | |||
| 1393 | my ($folder) = $window->pick( | ||
| 1394 | -checkoverwrite => 0, | ||
| 1395 | -showfolders => 1, | ||
| 1396 | -showfiles => 0, | ||
| 1397 | -selectmode => 'single', | ||
| 1398 | -selectstring => 'Select', | ||
| 1399 | -title => 'Select folder', | ||
| 1400 | ); | ||
| 1401 | |||
| 1402 | =back | ||
| 1403 | |||
| 1404 | =head1 AUTHOR | ||
| 1405 | |||
| 1406 | Hans Jeuken (hanje at cpan dot org) | ||
| 1407 | |||
| 1408 | =head1 LICENSE | ||
| 1409 | |||
| 1410 | Same as Perl. | ||
| 1411 | |||
| 1412 | =head1 BUGS | ||
| 1413 | |||
| 1414 | If you find any, please contact the author. | ||
| 1415 | |||
| 1416 | =cut | ||
| 1417 | |||
| 1418 | =head1 SEE ALSO | ||
| 1419 | |||
| 1420 | =over 4 | ||
| 1421 | |||
| 1422 | =item L<Tk::AppWindow::OverView> | ||
| 1423 | |||
| 1424 | =item L<Tk::AppWindow::CookBook> | ||
| 1425 | |||
| 1426 | =item L<Tk::AppWindow::BaseClasses::Extension> | ||
| 1427 | |||
| 1428 | =item L<Tk::AppWindow::Commands> | ||
| 1429 | |||
| 1430 | =item L<Tk::AppWindow::ConfigVariables> | ||
| 1431 | |||
| 1432 | =back | ||
| 1433 | |||
| 1434 | =cut | ||
| 1435 | |||
| 1436 | 1; | ||
| 1437 | __END__ |