]> scm.dxcluster.org Git - spider.git/blob - gtkconsole/Screen.pm
cba198338a1506a8c5b5c4bc4f983f47f47f24a5
[spider.git] / gtkconsole / Screen.pm
1 #
2 # Generic screen generator
3
4 # This produces the Gtk for all the little sub-screens
5 #
6 #
7 #
8 # Copyright (c) 2006 Dirk Koopman G1TLH
9 #
10
11 use strict;
12
13 package Screen;
14
15 use Gtk2;
16 use Gtk2::SimpleList;
17 use Text::Wrap;
18
19 INIT {
20         Gtk2::SimpleList->add_column_type( 'qrg',
21                                                                            type     => 'Glib::Scalar',
22                                                                            renderer => 'Gtk2::CellRendererText',
23                                                                            attr     => sub {
24                                                                                    my ($treecol, $cell, $model, $iter, $col_num) = @_;
25                                                                                    my $info = $model->get ($iter, $col_num);
26                                                                                    $cell->set(text => sprintf("%.1f", $info), xalign => 1.0);
27                                                                            }
28                                                                          );
29         
30         
31         Gtk2::SimpleList->add_column_type( 'tt',
32                                                                            type     => 'Glib::Scalar',
33                                                                            renderer => 'Gtk2::CellRendererText',
34                                                                            attr     => sub {
35                                                                                    my ($treecol, $cell, $model, $iter, $col_num) = @_;
36                                                                                    my $info = $model->get ($iter, $col_num);
37                                                                                    $cell->set(text => $info);
38                                                                            }
39                                                                          );
40
41         Gtk2::SimpleList->add_column_type( 'ttlong',
42                                                                            type     => 'Glib::Scalar',
43                                                                            renderer => 'Gtk2::CellRendererText',
44                                                                            attr     => sub {
45                                                                                    my ($treecol, $cell, $model, $iter, $col_num) = @_;
46                                                                                    my $info = $model->get ($iter, $col_num);
47                                                                                    $Text::Wrap::columns = 80;
48                                                                                    $cell->set(text => join("\n",wrap("","",$info)));
49                                                                            }
50                                                                          );
51
52         Gtk2::SimpleList->add_column_type( 'ttlesslong',
53                                                                            type     => 'Glib::Scalar',
54                                                                            renderer => 'Gtk2::CellRendererText',
55                                                                            attr     => sub {
56                                                                                    my ($treecol, $cell, $model, $iter, $col_num) = @_;
57                                                                                    my $info = $model->get ($iter, $col_num);
58                                                                                    $Text::Wrap::columns = 65;
59                                                                                    $cell->set(text => join("\n",wrap("","",$info)));
60                                                                            }
61                                                                          );
62
63         Gtk2::SimpleList->add_column_type( 'ttshort',
64                                                                            type     => 'Glib::Scalar',
65                                                                            renderer => 'Gtk2::CellRendererText',
66                                                                            attr     => sub {
67                                                                                    my ($treecol, $cell, $model, $iter, $col_num) = @_;
68                                                                                    my $info = $model->get ($iter, $col_num);
69                                                                                    $Text::Wrap::columns = 30;
70                                                                                    $cell->set(text => join("\n",wrap("","",$info)));
71                                                                            }
72                                                                          );
73
74 }
75
76 sub new
77 {
78         my $pkg = shift;
79         my $class = ref $pkg || $pkg;
80         return bless {@_}, $class;
81 }
82
83 sub widget
84 {
85         return $_[0]->{widget};
86 }
87
88 1;
89
90 package Screen::List;
91
92 our @ISA = qw(Screen);
93
94 sub _row_inserted
95 {
96         my ($liststore, $path, $iter, $self) = @_;
97         $self->{list}->scroll_to_cell($path);
98 }
99
100 sub new
101 {
102         my $pkg = shift;
103         my %args = @_;
104         
105         my $list = Gtk2::SimpleList->new(@{$args{fields}});
106         $list->set_rules_hint(1) if $args{hint};
107         $list->set_name($args{pkgname} || __PACKAGE__);
108         
109         my $scroll = Gtk2::ScrolledWindow->new (undef, undef);
110         $scroll->set_shadow_type ($args{shadow_type} || 'etched-out');
111         $scroll->set_policy (exists $args{policy} ? @{$args{policy}} : qw(automatic automatic));
112         $scroll->set_size_request (@{$args{size}}) if exists $args{size};
113         $scroll->add($list);
114         $scroll->set_border_width(exists $args{border_width} ? $args{border_width} : 2);
115         
116         my $self = $pkg->SUPER::new(scroller => $scroll, list => $list, widget => $scroll, maxsize => ($args{maxsize} || 100));
117
118         $list->get_model->signal_connect('row-inserted', \&_row_inserted, $self);
119
120         if ($args{frame}) {
121                 my $frame = Gtk2::Frame->new($args{frame});
122                 $frame->add($scroll);
123                 $self->{widget} = $self->{frame} = $frame;
124         }
125         return $self;
126 }
127
128 sub add_data
129 {
130         my $self = shift;
131         my $list = $self->{list};
132         
133         push @{$list->{data}}, ref $_[0] ? $_[0] : [ @_ ];
134         shift @{$list->{data}} if @{$list->{data}} > $self->{maxsize};
135 }
136 1;