1# contains:
2#   Panel
3package Prima::Widgets;
4
5use strict;
6use warnings;
7use Prima::Const;
8use Prima::Classes;
9
10package Prima::Panel;
11use vars qw(@ISA);
12@ISA = qw(Prima::Widget);
13
14sub profile_default
15{
16	my $def = $_[0]-> SUPER::profile_default;
17	my %prf = (
18		ownerBackColor => 1,
19		raise          => 1,
20		borderWidth    => 1,
21		image          => undef,
22		imageFile      => undef,
23		zoom           => 1,
24	);
25	@$def{keys %prf} = values %prf;
26	return $def;
27}
28
29sub profile_check_in
30{
31	my ( $self, $p, $default) = @_;
32	$self-> SUPER::profile_check_in( $p, $default);
33	if ( defined $p-> {imageFile} && !defined $p-> {image}) {
34		$p-> {image} = Prima::Image-> create;
35		delete $p-> {image} unless $p-> {image}-> load($p-> {imageFile});
36	}
37}
38
39sub init
40{
41	my $self = shift;
42	for ( qw( image ImageFile))
43		{ $self-> {$_} = undef; }
44	for ( qw( zoom raise borderWidth iw ih)) { $self-> {$_} = 1; }
45	my %profile = $self-> SUPER::init(@_);
46	$self-> { imageFile}  = $profile{ imageFile};
47	for ( qw( image zoom borderWidth raise)) {
48		$self-> $_($profile{$_});
49	}
50	return %profile;
51}
52
53sub on_paint
54{
55	my ( $self, $canvas) = @_;
56	my @size   = $canvas-> size;
57
58	my $clr    = $self-> backColor;
59	my $bw     = $self-> {borderWidth};
60	my @c3d    = ( $self-> light3DColor, $self-> dark3DColor);
61	@c3d = reverse @c3d unless $self-> {raise};
62	my $cap = $self-> text;
63	unless ( defined $self-> {image}) {
64		$canvas-> rect3d( 0, 0, $size[0]-1, $size[1]-1, $bw, @c3d, $clr);
65		$canvas-> text_shape_out( $cap,
66			( $size[0] - $canvas-> get_text_width( $cap)) / 2,
67			( $size[1] - $canvas-> font-> height) / 2,
68		) if defined $cap;
69		return;
70	}
71	$canvas-> rect3d( 0, 0, $size[0]-1, $size[1]-1, $bw, @c3d) if $bw > 0;
72	my ( $x, $y) = ( $bw, $bw);
73	my ( $dx, $dy ) = ( $self-> {iw}, $self-> {ih});
74	$canvas-> clipRect( $bw, $bw, $size[0] - $bw - 1, $size[1] - $bw - 1) if $bw > 0;
75	$canvas-> clear if $self-> {image}-> isa('Prima::Icon');
76	while ( $x < $size[0] - $bw) {
77		$y = $bw;
78		while ( $y < $size[1] - $bw) {
79			$canvas-> stretch_image( $x, $y, $dx, $dy, $self-> {image});
80			$y += $dy;
81		}
82		$x += $dx;
83	}
84	$canvas-> text_shape_out( $cap,
85		( $size[0] - $canvas-> get_text_width( $cap)) / 2,
86		( $size[1] - $canvas-> font-> height) / 2,
87	) if $cap;
88}
89
90sub set_border_width
91{
92	my ( $self, $bw) = @_;
93	$bw = 0 if $bw < 0;
94	$bw = int( $bw);
95	return if $bw == $self-> {borderWidth};
96	$self-> {borderWidth} = $bw;
97	$self-> repaint;
98}
99
100sub set_text
101{
102	$_[0]-> SUPER::set_text( $_[1]);
103	$_[0]-> repaint;
104}
105
106
107sub set_raise
108{
109	my ( $self, $r) = @_;
110	return if $r == $self-> {raise};
111	$self-> {raise} = $r;
112	$self-> repaint;
113}
114
115sub set_image_file
116{
117	my ($self,$file,$img) = @_;
118	$img = Prima::Image-> create;
119	return unless $img-> load($file);
120	$self-> {imageFile} = $file;
121	$self-> image($img);
122}
123
124sub set_image
125{
126	my ( $self, $img) = @_;
127	$self-> {image} = $img;
128	return unless defined $img;
129	( $self-> {iw}, $self-> {ih}) = ($img-> width * $self-> {zoom}, $img-> height * $self-> {zoom});
130	$self-> {image} = undef if $self-> {ih} == 0 || $self-> {iw} == 0;
131	$self-> repaint;
132}
133
134
135sub set_zoom
136{
137	my ( $self, $zoom) = @_;
138	$zoom = int( $zoom);
139	$zoom = 1 if $zoom < 1;
140	$zoom = 10 if $zoom > 10;
141	return if $zoom == $self-> {raise};
142	$self-> {zoom} = $zoom;
143	return unless defined $self-> {image};
144	my $img = $self-> {img};
145	( $self-> {iw}, $self-> {ih}) = ( $img-> width * $self-> {zoom}, $img-> height * $self-> {zoom});
146	$self-> repaint;
147}
148
149sub image        {($#_)?$_[0]-> set_image($_[1]):return $_[0]-> {image} }
150sub imageFile    {($#_)?$_[0]-> set_image_file($_[1]):return $_[0]-> {imageFile}}
151sub zoom         {($#_)?$_[0]-> set_zoom($_[1]):return $_[0]-> {zoom}}
152sub borderWidth  {($#_)?$_[0]-> set_border_width($_[1]):return $_[0]-> {borderWidth}}
153sub raise        {($#_)?$_[0]-> set_raise($_[1]):return $_[0]-> {raise}}
154
1551;
156
157=pod
158
159=head1 NAME
160
161Prima::Widgets - miscellaneous widget classes
162
163=head1 DESCRIPTION
164
165The module was designed to serve as a collection of small widget
166classes that do not group well with the other, more purposeful classes.
167The current implementation contains the only class, C<Prima::Panel>.
168
169=head1 Prima::Panel
170
171Provides a simple panel widget, capable of displaying a single line
172of centered text on a custom background. Probably this functionality
173is better to be merged into C<Prima::Label>'s.
174
175=head2 Properties
176
177=over
178
179=item borderWidth INTEGER
180
181Width of 3d-shade border around the widget.
182
183Default value: 1
184
185=item image OBJECT
186
187Selects image to be drawn as a tiled background.
188If C<undef>, the background is drawn with the background color.
189
190=item imageFile PATH
191
192Set the image FILE to be loaded and displayed. Is rarely used since does not return
193a loading success flag.
194
195=item raise BOOLEAN
196
197Style of 3d-shade border around the widget.
198If 1, the widget is 'risen'; if 0 it is 'sunken'.
199
200Default value: 1
201
202=item zoom INTEGER
203
204Selects zoom level for image display.
205The acceptable value range is between 1 and 10.
206
207Default value: 1
208
209=back
210
211=head1 AUTHOR
212
213Dmitry Karasik, E<lt>dmitry@karasik.eu.orgE<gt>.
214
215=head1 SEE ALSO
216
217L<Prima>
218
219=cut
220