| 1 | ######################################## SOH ########################################### | ||
| 2 | ## Function : Wrapper for the Tk::SplashScreenZ based Boot-Splash procedures | ||
| 3 | ## | ||
| 4 | ## Copyright (c) 2003 Michael Krause. All rights reserved. | ||
| 5 | ## This program is free software; you can redistribute it and/or modify it | ||
| 6 | ## under the same terms as Perl itself. | ||
| 7 | ## | ||
| 8 | ## History : V0.1 05-Dec-2003 Derived from Tavern. MK | ||
| 9 | ## V0.2 14-Jan-2004 Added some documentation. MK | ||
| 10 | ## | ||
| 11 | ######################################## EOH ########################################### | ||
| 12 | package Tk::BootSplash; | ||
| 13 | |||
| 14 | require 5.005_62; | ||
| 15 | use Tk 800.023; | ||
| 16 | |||
| 17 | require Exporter; | ||
| 18 | |||
| 19 | our @ISA = qw(Exporter); | ||
| 20 | |||
| 21 | # Default-Exported Functions | ||
| 22 | our @EXPORT = qw(SetupBootSplash SetBootPhase | ||
| 23 | WaitSplash UpdateSplash FinalizeSplash); | ||
| 24 | |||
| 25 | ############################################## | ||
| 26 | ### Use | ||
| 27 | ############################################## | ||
| 28 | use strict; | ||
| 29 | use vars qw($VERSION); | ||
| 30 | |||
| 31 | $VERSION = '0.2'; | ||
| 32 | # | ||
| 33 | use Carp; | ||
| 34 | # graphical stuff | ||
| 35 | #use Tk::widgets qw/Label Canvas ProgressBar/; | ||
| 36 | use Tk::widgets qw/Label Canvas/; # Note: We use here the enhanced Progressbar which is NOT yet part of std. Perl-Tk | ||
| 37 | use Tk::ProgressBarPlus; | ||
| 38 | |||
| 39 | BEGIN { | ||
| 40 | eval { | ||
| 41 | require Tk::SplashScreenZ; | ||
| 42 | import Tk::SplashScreenZ; | ||
| 43 | }; | ||
| 44 | if ($@) { # ups, no Tk::SplashScreenZ, use this one here | ||
| 45 | ######################################################################## | ||
| 46 | package Tk::SplashScreenZ; | ||
| 47 | ######################################################################## | ||
| 48 | $Tk::SplashScreenZ::VERSION = '1.0'; | ||
| 49 | use Tk qw/Ev/; | ||
| 50 | use Tk qw/:eventtypes/; | ||
| 51 | use Tk::waitVariableX; | ||
| 52 | use Tk::widgets qw/Toplevel/; | ||
| 53 | use base qw/Tk::Toplevel/; | ||
| 54 | |||
| 55 | Construct Tk::Widget 'SplashScreenZ'; | ||
| 56 | |||
| 57 | sub Populate { | ||
| 58 | my ($self, $args) = @_; | ||
| 59 | |||
| 60 | $self->withdraw; | ||
| 61 | $self->overrideredirect(1); | ||
| 62 | |||
| 63 | $self->SUPER::Populate($args); | ||
| 64 | |||
| 65 | $self->{ofx} = 0; # X offset from top-left corner to cursor | ||
| 66 | $self->{ofy} = 0; # Y offset from top-left corner to cursor | ||
| 67 | $self->{tm0} = 0; # microseconds time widget was Shown | ||
| 68 | |||
| 69 | $self->ConfigSpecs( | ||
| 70 | -milliseconds => [qw/PASSIVE milliseconds Milliseconds 0/], | ||
| 71 | ); | ||
| 72 | |||
| 73 | $self->bind('<ButtonPress-3>' => [$self => 'b3prs', Ev('x'), Ev('y')]); | ||
| 74 | $self->bind('<ButtonRelease-3>' => [$self => 'b3rls', Ev('X'), Ev('Y')]); | ||
| 75 | |||
| 76 | } # end Populate | ||
| 77 | |||
| 78 | # Object methods. | ||
| 79 | |||
| 80 | sub Destroy { | ||
| 81 | |||
| 82 | |||
| 83 | my ($self, $millis) = @_; | ||
| 84 | |||
| 85 | $millis = $self->cget(-milliseconds) unless defined $millis; | ||
| 86 | my $t = Tk::timeofday; | ||
| 87 | $millis = $millis - ( ($t - $self->{tm0}) * 1000 ); | ||
| 88 | $millis = 0 if $millis < 0; | ||
| 89 | |||
| 90 | my $destroy_splashscreen = sub { | ||
| 91 | $self->update; | ||
| 92 | $self->after(100); # ensure 100% of PB seen | ||
| 93 | $self->destroy; | ||
| 94 | }; | ||
| 95 | |||
| 96 | do { &$destroy_splashscreen; return } if $millis == 0; | ||
| 97 | |||
| 98 | while ( $self->DoOneEvent (DONT_WAIT | TIMER_EVENTS)) {} | ||
| 99 | |||
| 100 | $self->waitVariableX( [$millis, $destroy_splashscreen] ); | ||
| 101 | |||
| 102 | } # end Destroy | ||
| 103 | |||
| 104 | sub Splash { | ||
| 105 | |||
| 106 | my ($self, $millis) = @_; | ||
| 107 | |||
| 108 | $millis = $self->cget(-milliseconds) unless defined $millis; | ||
| 109 | $self->{tm0} = Tk::timeofday; | ||
| 110 | $self->configure(-milliseconds => $millis); | ||
| 111 | $self->Popup; | ||
| 112 | |||
| 113 | } # end_splash | ||
| 114 | |||
| 115 | # Private methods. | ||
| 116 | |||
| 117 | sub b3prs { | ||
| 118 | my ($self, $x, $y) = @_; | ||
| 119 | $self->{ofx} = $x; | ||
| 120 | $self->{ofy} = $y; | ||
| 121 | } # end b3prs | ||
| 122 | |||
| 123 | sub b3rls { | ||
| 124 | my($self, $X, $Y) = @_; | ||
| 125 | $X -= $self->{ofx}; | ||
| 126 | $Y -= $self->{ofy}; | ||
| 127 | $self->geometry("+${X}+${Y}"); | ||
| 128 | } # end b3rls | ||
| 129 | |||
| 130 | 1; | ||
| 131 | } | ||
| 132 | } | ||
| 133 | ######################################################################## | ||
| 134 | |||
| 135 | ############################################## | ||
| 136 | # Local Variables | ||
| 137 | ############################################## | ||
| 138 | my ($window, $parent, $canvas, | ||
| 139 | $boot_percentage, $bootpercentage_border, | ||
| 140 | $boot_cb_id, $bootpicture, $boot_phase, $update_ongoing); | ||
| 141 | |||
| 142 | ######################################################################## | ||
| 143 | # boot splash functions | ||
| 144 | ######################################################################## | ||
| 145 | |||
| 146 | # -------------------------------------------------------- | ||
| 147 | # Creates and initializes a Boot-Splash screen. | ||
| 148 | # -------------------------------------------------------- | ||
| 149 | sub SetupBootSplash | ||
| 150 | { | ||
| 151 | my (%args) = @_; | ||
| 152 | #my ($myparent, $app_name) = @_; | ||
| 153 | |||
| 154 | # locals | ||
| 155 | my (@valid_keys, %valid_keys, $arg); | ||
| 156 | |||
| 157 | # Check Args | ||
| 158 | @valid_keys = qw(-parent -width -height -application_name -size -totaltime -image -x1 -y1 -x2 -y2 -x3 -y3); | ||
| 159 | %valid_keys = map {$_ => '1'} @valid_keys; | ||
| 160 | foreach $arg (keys %args) { | ||
| 161 | croak "Undefined Key [$arg] (only [@valid_keys] are valid)!\n" unless $valid_keys{$arg}; | ||
| 162 | } | ||
| 163 | croak "No Application Name for Bootsplash defined!\n" unless $args{-application_name}; | ||
| 164 | croak "No Image for Bootsplash defined!\n" unless $args{-image}; | ||
| 165 | $parent = delete $args{-parent}; croak "No Parent Window for Bootsplash defined!\n" unless $parent; | ||
| 166 | |||
| 167 | $window = $parent->SplashScreenZ( | ||
| 168 | -milliseconds => $args{-totaltime} || 5000, | ||
| 169 | ); | ||
| 170 | $canvas = $window->Canvas( | ||
| 171 | -width => $args{-width}, | ||
| 172 | -height => $args{-height}, | ||
| 173 | )->pack(); | ||
| 174 | |||
| 175 | # Reset the counter | ||
| 176 | $bootpercentage_border = 0; | ||
| 177 | |||
| 178 | # Fill it | ||
| 179 | $canvas->createImage(0,0, | ||
| 180 | -image => $args{-image}, | ||
| 181 | -anchor => 'nw', | ||
| 182 | ); | ||
| 183 | #------------------------------------------------ | ||
| 184 | $canvas->createText($args{-x1}+2, $args{-y1}+2, | ||
| 185 | -font => 'Helvetica -16 bold', | ||
| 186 | -text => 'Booting ...', | ||
| 187 | -fill => 'black', | ||
| 188 | -anchor => 'nw', | ||
| 189 | ); | ||
| 190 | $canvas->createText($args{-x1}, $args{-y1}, | ||
| 191 | -font => 'Helvetica -16 bold', | ||
| 192 | -text => 'Booting ...', | ||
| 193 | -fill => 'white', | ||
| 194 | -anchor => 'nw', | ||
| 195 | ); | ||
| 196 | #------------------------------------------------ | ||
| 197 | $canvas->createText($args{-x2}+2, $args{-y2}+2, | ||
| 198 | -font => 'Helvetica -' . $args{-size} . ' bold', | ||
| 199 | -text => $args{-application_name}, | ||
| 200 | -fill => 'black', | ||
| 201 | -stipple => (lc($Tk::platform) eq 'mswin32') ? undef : 'gray50', | ||
| 202 | -anchor => 'center', | ||
| 203 | ); | ||
| 204 | $canvas->createText($args{-x2}, $args{-y2}, | ||
| 205 | -font => 'Helvetica -' . $args{-size} . ' bold', | ||
| 206 | -text => $args{-application_name}, | ||
| 207 | -fill => 'white', | ||
| 208 | -stipple => (lc($Tk::platform) eq 'mswin32') ? undef : 'gray50', | ||
| 209 | -anchor => 'center', | ||
| 210 | ); | ||
| 211 | #------------------------------------------------ | ||
| 212 | $canvas->createText($args{-x3}+2, $args{-y3}+2, | ||
| 213 | -font => 'Helvetica -12 bold', | ||
| 214 | -text => 'Bootphase:', | ||
| 215 | -fill => 'black', | ||
| 216 | -anchor => 'nw', | ||
| 217 | -tags => 'bootphase1', | ||
| 218 | ); | ||
| 219 | $canvas->createText($args{-x3}, $args{-y3}, | ||
| 220 | -font => 'Helvetica -12 bold', | ||
| 221 | -text => 'Bootphase:', | ||
| 222 | -fill => 'white', | ||
| 223 | -anchor => 'nw', | ||
| 224 | -tags => 'bootphase2', | ||
| 225 | ); | ||
| 226 | #------------------------------------------------ | ||
| 227 | my $progressbar = $canvas->ProgressBarPlus( | ||
| 228 | -borderwidth => '2', | ||
| 229 | -relief => 'groove', | ||
| 230 | -from => '0', | ||
| 231 | -to => '100', | ||
| 232 | -blocks => '100', | ||
| 233 | -colors => [0, 'blue' ], | ||
| 234 | -variable => \$boot_percentage, | ||
| 235 | -showvalue => '1', | ||
| 236 | #-valuecolor => 'yellow', | ||
| 237 | ); | ||
| 238 | |||
| 239 | $canvas->createWindow(0, $args{-height}-16, | ||
| 240 | -window => $progressbar, | ||
| 241 | -anchor => 'nw', | ||
| 242 | -width => $args{-width}, | ||
| 243 | -tags => 'progressbar', | ||
| 244 | ); | ||
| 245 | |||
| 246 | # display the Splash, if we want to have it | ||
| 247 | if ($boot_percentage < 100 ) { | ||
| 248 | $bootpercentage_border = $boot_percentage = 0; | ||
| 249 | #setup the counter-callback | ||
| 250 | $boot_cb_id = $parent-> repeat(10, \&boot_cb); | ||
| 251 | $window->update; | ||
| 252 | $window->Splash; | ||
| 253 | } | ||
| 254 | return $bootpicture; | ||
| 255 | } | ||
| 256 | |||
| 257 | # -------------------------------------------------------- | ||
| 258 | # Sets the current Bootphase and the | ||
| 259 | # belonging border in percent (0..100). | ||
| 260 | # -------------------------------------------------------- | ||
| 261 | sub SetBootPhase | ||
| 262 | { | ||
| 263 | my ($new_bootphase, $new_border) = @_; | ||
| 264 | $boot_phase = $new_bootphase; | ||
| 265 | $bootpercentage_border = $new_border; | ||
| 266 | $bootpercentage_border = 100 if $bootpercentage_border > 100; | ||
| 267 | } | ||
| 268 | |||
| 269 | # -------------------------------------------------------- | ||
| 270 | # Helper Function for the Boot-Splash-Screen Progress | ||
| 271 | # display. It will delay the execution for faster | ||
| 272 | # setup operations. | ||
| 273 | # -------------------------------------------------------- | ||
| 274 | sub WaitSplash | ||
| 275 | { | ||
| 276 | return unless $window; | ||
| 277 | while ($boot_percentage <= $bootpercentage_border) { | ||
| 278 | $window->update; | ||
| 279 | $window->after(20); | ||
| 280 | } | ||
| 281 | } | ||
| 282 | |||
| 283 | # -------------------------------------------------------- | ||
| 284 | # Helper Function for the Boot-Splash-Screen Progress | ||
| 285 | # display. It will show current stage on screen. | ||
| 286 | # -------------------------------------------------------- | ||
| 287 | sub UpdateSplash | ||
| 288 | { | ||
| 289 | $window->update; | ||
| 290 | } | ||
| 291 | |||
| 292 | # -------------------------------------------------------- | ||
| 293 | # Helper Function for the Boot-Splash-Screen finalization. | ||
| 294 | # -------------------------------------------------------- | ||
| 295 | sub FinalizeSplash | ||
| 296 | { | ||
| 297 | #Finalize the Splash, if we had one | ||
| 298 | $window->update; | ||
| 299 | if (defined $boot_cb_id ) { | ||
| 300 | $parent->after(900); | ||
| 301 | $window->Destroy; | ||
| 302 | $boot_cb_id->cancel; | ||
| 303 | } | ||
| 304 | else { | ||
| 305 | $parent->after(900); | ||
| 306 | $parent->update; | ||
| 307 | } | ||
| 308 | |||
| 309 | # Wait for the Splash to finish | ||
| 310 | SetBootPhase('Setup complete ...', 100); | ||
| 311 | } | ||
| 312 | |||
| 313 | # -------------------------------------------------------- | ||
| 314 | # INTERNAL FUNCTION: | ||
| 315 | # Callback for updating the graphics of the | ||
| 316 | # Boot-Splash-Screen's progress display. | ||
| 317 | # -------------------------------------------------------- | ||
| 318 | sub boot_cb | ||
| 319 | { | ||
| 320 | if ($boot_percentage <= $bootpercentage_border) { | ||
| 321 | $boot_percentage++; | ||
| 322 | } | ||
| 323 | if ($canvas) { | ||
| 324 | $canvas->dchars('bootphase1', 0, 'end'); | ||
| 325 | $canvas->insert('bootphase1', 0, $boot_phase); | ||
| 326 | $canvas->dchars('bootphase2', 0, 'end'); | ||
| 327 | $canvas->insert('bootphase2', 0, $boot_phase); | ||
| 328 | } | ||
| 329 | return if $update_ongoing; | ||
| 330 | $update_ongoing = 1; | ||
| 331 | $window->update; | ||
| 332 | $update_ongoing = undef; | ||
| 333 | } | ||
| 334 | ######################################################################## | ||
| 335 | 1; | ||
| 336 | __END__ |