1=pod
2
3=head1 NAME
4
5examples/grid.pl - Prima grid widget example
6
7=head1 FEATURES
8
9Demonstrates the usage of grid widgets - Prima::AbstractGrid and
10its text-oriented descendant, Prima::Grid. To switch between
11these, toggle $abstract_grid flag.
12
13=cut
14
15
16use strict;
17use warnings;
18use Prima;
19use Prima::Application;
20use Prima::Grids;
21
22
23my $g;
24my $w = Prima::MainWindow-> create(
25	text => 'Grid example',
26	packPropagate => 0,
27	menuItems => [
28		['~Options' => [
29			['*dhg', 'Draw HGrid'=> sub { $g-> drawHGrid( $_[0]-> menu-> toggle( $_[1])) }],
30			['*dvg', 'Draw VGrid'=> sub { $g-> drawVGrid( $_[0]-> menu-> toggle( $_[1])) }],
31			['mse', 'Multi select'=> sub { $g-> multiSelect( $_[0]-> menu-> toggle( $_[1])) }],
32			['*ind', 'Use indents' => sub { $g-> cellIndents(($_[0]-> menu-> toggle( $_[1])) x 4); }],
33			['ccw', 'Constant cell width' => sub { $g-> constantCellWidth($_[0]-> menu-> toggle( $_[1]) ? 100 : undef); }],
34			['cch', 'Constant cell height' => sub { $g-> constantCellHeight($_[0]-> menu-> toggle( $_[1]) ? 100 : undef); }],
35		]
36	]],
37);
38
39# change this to 0 and Prima::Grid will be created instead
40my $abstract_grid = 1;
41
42my @user_breadths=({},{});
43
44$g = $w-> insert(
45( $abstract_grid ? 'Prima::AbstractGrid' : 'Prima::Grid' ),
46( $abstract_grid ? () : ('cells', [ map { my $a = $_; [ map {"$a.$_"} 0 .. 300]} 0 .. 300])),
47	onMeasure => sub {
48		my ( $self, $axis, $index, $ref) = @_;
49		if ( defined $user_breadths[$axis]-> {$index} ) {
50			$$ref = $user_breadths[$axis]-> {$index};
51		} else {
52			$$ref = ( 11 - ( $index % 11)) * 15;
53			$$ref = 15 if $$ref < 15;
54		}
55	},
56	onSetExtent => sub {
57		my ( $self, $axis, $index, $breadth) = @_;
58		$user_breadths[$axis]-> {$index} = $breadth;
59	},
60	onDrawCell => sub {
61		my ( $self, $canvas,
62				$col, $row, $type,
63				$x1, $y1, $x2, $y2,
64				$X1, $Y1, $X2, $Y2,
65				$sel, $foc, $pre) = @_;
66
67		my $bk = $sel ? $self-> hiliteBackColor :
68						( $type ? $self-> indentCellBackColor : cl::Back);
69		$bk = $self-> prelight_color($bk) if $pre;
70		$canvas-> backColor( $bk );
71		$canvas-> clear( $x1, $y1, $x2, $y2);
72		$canvas-> color( $sel ? $self-> hiliteColor :
73						( $type ? $self-> indentCellColor : cl::Fore));
74		$canvas-> text_out( "$col.$row", $X2-50, $Y1+3);
75		$canvas-> rect_focus( $x1, $y1, $x2, $y2 ) if $foc;
76	},
77	onGetRanges => sub {
78		my ( $self, $axis, $index, $min, $max) = @_;
79		$$min = 50;
80	},
81	onStringify => sub {
82		my ( $self, $col, $row, $ref) = @_;
83		$$ref = "$col.$row";
84	},
85	onGetAlignment => sub {
86		my ( $self, $col, $row, $ha, $va) = @_;
87		$$ha = ta::Center if $col == 0 || $row == 0 || $col == 9999 || $row == 9999;
88	},
89	allowChangeCellWidth => 1,
90	allowChangeCellHeight => 1,
91	clipCells => 2,
92	pack => { expand => 1, fill => 'both' },
93);
94if ( $abstract_grid) {
95	$g-> columns(10000);
96	$g-> rows(10000);
97}
98$g-> cellIndents(1,1,1,1);
99
100#$g-> insert_row( 0, 0..300 );
101#$g-> insert_column( 100, 0..300 );
102#$g-> delete_columns( 100, 1);
103#$g-> add_column( 100, [0..300] );
104
105run Prima;
106