File Coverage

blib/lib/Tk/FastSplash.pm
Criterion Covered Total %
statement 10 72 13.8
branch 2 38 5.2
condition n/a
subroutine 1 9 11.1
pod 0 4 0.0
total 13 123 10.5


line stmt bran cond sub pod time code
1             # -*- perl -*-
2              
3             #
4             # $Id: FastSplash.pm,v 1.20 2005/08/25 22:14:45 eserte Exp $
5             # Author: Slaven Rezic
6             #
7             # Copyright (C) 1999,2003,2005 Slaven Rezic. All rights reserved.
8             # This package is free software; you can redistribute it and/or
9             # modify it under the same terms as Perl itself.
10             #
11             # Mail: srezic@cpan.org
12             # WWW: http://www.rezic.de/eserte/
13             #
14              
15             package Tk::FastSplash;
16             #use strict;use vars qw($TK_VERSION $VERSION);
17             $VERSION = $VERSION = "0.15";
18             $TK_VERSION = 800 if !defined $TK_VERSION;
19              
20             sub Show {
21 3     3 0 1059 my($pkg,
22             $image_file, $image_width, $image_height, $title, $override) = @_;
23 3 50       14 $title = $0 if !defined $title;
24 3         6 my $splash_screen = {};
25 3         7 eval {
26             package
27             Tk; # hide from indexer
28 3         22 require DynaLoader;
29 3         188 eval q{ require Tk::Event };
30 3         72 @Tk::ISA = qw(DynaLoader);
31 3         13450 bootstrap Tk;
32 0     0     sub TranslateFileName { $_[0] }
33 0     0     sub SplitString { split /\s+/, $_[0] } # rough approximation
34              
35 0 0       0 if (Tk::FontRankInfo->can("encoding")) {
36 0         0 $Tk::FastSplash::TK_VERSION = 804;
37             }
38              
39 0 0       0 if ($Tk::FastSplash::TK_VERSION < 804) {
40             package
41             Tk::Photo; # hide from indexer
42 0         0 @Tk::Photo::ISA = qw(DynaLoader);
43 0         0 bootstrap Tk::Photo;
44             }
45              
46 0 0       0 if ($Tk::FastSplash::TK_VERSION >= 804) {
47 0         0 *Tk::getEncoding = \&Tk::FastSplash::getEncoding;
48             }
49              
50             package Tk::FastSplash;
51 0     0     sub _Destroyed { }
52 0         0 $splash_screen = Tk::MainWindow::Create(".", $title);
53 0         0 bless $splash_screen, 'Tk::MainWindow';
54 0         0 $splash_screen->{"Exists"} = 1;
55              
56 0 0       0 if ($override) {
57 0         0 require Tk::Wm;
58 0         0 $splash_screen->overrideredirect(1);
59             }
60              
61 0         0 my $img = Tk::image($splash_screen, 'create', 'photo', 'splashphoto',
62             -file => $image_file);
63 0         0 bless $img, 'Tk::Image';
64 0         0 $splash_screen->{Photo} = $img;
65 0 0       0 $image_width = $img->width if !defined $image_width;
66 0 0       0 $image_height = $img->height if !defined $image_height;
67 0         0 my $sw = Tk::winfo($splash_screen, 'screenwidth');
68 0         0 my $sh = Tk::winfo($splash_screen, 'screenheight');
69 0         0 Tk::wm($splash_screen, "geometry",
70             "+" . int($sw/2 - $image_width/2) .
71             "+" . int($sh/2 - $image_height/2));
72              
73 0         0 $splash_screen->{ImageWidth} = $image_width;
74              
75 0 0       0 my(@fontarg) = ($TK_VERSION >= 800
76             # dummy font to satisfy SplitString
77             ? (-font => "Helvetica 10")
78             # no font for older Tk's
79             : ());
80 0         0 my $l_path = '.splashlabel';
81 0         0 my $l = Tk::label($splash_screen, $l_path,
82             @fontarg,
83             -bd => 0,
84             -image => 'splashphoto');
85 0 0       0 if (!ref $l) {
86             # >= Tk803
87 0         0 $l = Tk::Widget::Widget($splash_screen, $l);
88             }
89 0         0 $l->{'_TkValue_'} = $l_path;
90 0         0 bless $l, 'Tk::Widget';
91 0         0 Tk::pack($l, -fill => 'both', -expand => 1);
92 0         0 Tk::update($splash_screen);
93             };
94 3 50       269 warn $@ if $@;
95 3         25 bless $splash_screen, $pkg;
96             }
97              
98             sub Raise {
99 0     0 0   my $w = shift;
100 0 0         if ($w->{"Exists"}) {
101 0     0     Tk::catch(sub { Tk::raise($w) });
  0            
102             }
103             }
104              
105             sub Destroy {
106 0     0 0   my $w = shift;
107 0 0         if ($w->{Photo}) {
108 0           $w->{Photo}->delete;
109 0           undef $w->{Photo};
110             }
111 0 0         if ($w->{"Exists"}) {
112 0     0     Tk::catch(sub { Tk::destroy($w) });
  0            
113             }
114             }
115              
116             # Taken from Tk.pm (Tk804.025_beta6)
117             sub getEncoding
118             {
119 0     0 0   my ($class,$name) = @_;
120              
121 0           eval { require Encode };
  0            
122 0 0         if ($@)
123             {
124 0           require Tk::DummyEncode;
125 0           return Tk::DummyEncode->getEncoding($name);
126             }
127              
128 0           $Tk::encodeStopOnError = Encode::FB_QUIET();
129 0           $Tk::encodeFallback = Encode::FB_PERLQQ(); # Encode::FB_DEFAULT();
130              
131 0 0         $name = $Tk::font_encoding{$name} if exists $Tk::font_encoding{$name};
132 0           my $enc = Encode::find_encoding($name);
133              
134 0 0         unless ($enc)
135             {
136 0 0         $enc = Encode::find_encoding($name) if ($name =~ s/[-_]\d+$//)
137             }
138             # if ($enc)
139             # {
140             # print STDERR "Lookup '$name' => ".$enc->name."\n";
141             # }
142             # else
143             # {
144             # print STDERR "Failed '$name'\n";
145             # }
146 0 0         unless ($enc)
147             {
148 0 0         if ($name eq 'X11ControlChars')
149             {
150 0           require Tk::DummyEncode;
151 0           $Encode::encoding{$name} = $enc = Tk::DummyEncode->getEncoding($name);
152             }
153             }
154 0           return $enc;
155             }
156              
157              
158              
159              
160             1;
161              
162             =head1 NAME
163              
164             Tk::FastSplash - create a fast starting splash screen
165              
166             =head1 SYNOPSIS
167              
168             BEGIN {
169             require Tk::FastSplash;
170             $splash = Tk::FastSplash->Show($image, $width, $height, $title,
171             $overrideredirect);
172             }
173             ...
174             use Tk;
175             ...
176             $splash->Destroy if $splash;
177             MainLoop;
178              
179             =head1 DESCRIPTION
180              
181             This module creates a fast loading splash screen for Perl/Tk programs.
182             It uses lowlevel Perl/Tk stuff, so upward compatibility is not given
183             (the module should work at least for Tk800.015, .022, .024, .025 and
184             Tk804.025, but does not work with newer ActivePerl versions).
185              
186             The splash screen is created with the B function. Supplied
187             arguments are: filename of the displayed image, width and height of
188             the image and the string for the title bar. I<$width> and I<$height>
189             may be left undefined. If I<$overrideredirect> is set to a true value,
190             then the splash screen will come without window manager decoration. If
191             something goes wrong, then B will silently ignore all errors and
192             continue without a splash screen. The splash screen can be destroyed
193             with the B method, best short before calling B.
194              
195             If you want to run this module on a Tk402.xxx system, then you have to
196             set the variable C<$Tk::FastSplash::TK_VERSION> to a value less than
197             800.
198              
199             I<$image> should be one of the core Perl/Tk image types (gif, ppm,
200             bmp). For jpegs and pngs, a C or C prior to
201             the call of the C method would be necessary, but calling one of
202             these two would slurp the whole Tk module in, making the point of
203             BSplash useless.
204              
205             =head1 CAVEAT
206              
207             This module does forbidden things e.g. bootstrapping the Tk shared
208             object or poking in the Perl/Tk internals. Because of this, this
209             module can stop working in a new Perl/Tk release. If you are concerned
210             about compatibility, then you should use L instead. If
211             your primary concern is speed, then C is for you (and
212             the primary reason I wrote this module). The splash window of
213             C should pop up 1 or 2 seconds faster than using
214             L or a vanilla L window.
215              
216             =head1 BUGS
217              
218             Probably many.
219              
220             If used with newer ActivePerl (e.g. build 811), then it is possible
221             that the application becomes unusable by using strange characters.
222              
223             You cannot call C twice in one application.
224              
225             The $^W variable should be turned off until the "use Tk" call.
226              
227             If FastSplash is executed in a BEGIN block (which is recommended for
228             full speed), then strange things will happen when using C or
229             trying to compile a script: the splash screen will always pop up while
230             doing those things. Therefore it is recommended to disable the splash
231             screen in check or debug mode:
232              
233             BEGIN {
234             if (!$^C && !$^P) {
235             require Tk::FastSplash;
236             $splash = Tk::FastSplash->Show($image, $width, $height, $title,
237             $overrideredirect);
238             }
239             }
240              
241             The -display switch is not honoured (but setting the environment
242             variable DISPLAY will work).
243              
244             XXX Avoid Win32 raise/lower problem with this code (maybe)?
245              
246             # Windows constants
247             my ($ONTOP, $NOTOP, $TOP) = (-1, -2, 0);
248             my ($SWP_NOMOVE, $SWP_NOSIZE) = (2, 1);
249            
250             my $SetWindowPos = new Win32::API("user32", "SetWindowPos", 'NNNNNNN', 'N');
251             my $FindWindow = new Win32::API("user32", "FindWindow", 'PP', 'N');
252            
253             # Reestablish Z order
254             my $class = "TkTopLevel";
255             my $topHwnd = $FindWindow->Call($class, $w->title);
256             $topHwnd and $SetWindowPos->Call($topHwnd, $ONTOP, 0, 0, 0, 0, $SWP_NOMOVE | $SWP_NOSIZE);
257              
258              
259             =head1 AUTHOR
260              
261             Slaven Rezic (slaven@rezic.de)
262              
263             =head1 SEE ALSO
264              
265             L, L, L,
266             L.
267              
268             =cut
269              
270             __END__