| 1 | # You may distribute under the terms of the GNU General Public License | ||
| 2 | # | ||
| 3 | # (C) Paul Evans, 2008-2013 -- leonerd@leonerd.org.uk | ||
| 4 | |||
| 5 | package Circle::FE::Gtk::Tab; | ||
| 6 | |||
| 7 | use strict; | ||
| 8 | use warnings; | ||
| 9 | |||
| 10 | use Glib qw( TRUE FALSE ); | ||
| 11 | |||
| 12 | use File::ShareDir qw( dist_file ); | ||
| 13 | |||
| 14 | use Variable::Disposition qw( retain_future ); | ||
| 15 | use Module::Pluggable search_path => "Circle::FE::Gtk::Widget", | ||
| 16 | sub_name => "widgets", | ||
| 17 | require => 1; | ||
| 18 | |||
| 19 | our $current_tab; | ||
| 20 | |||
| 21 | sub gen_menu | ||
| 22 | { | ||
| 23 | return [ | ||
| 24 | 'Scroll to Top' => { | ||
| 25 | accelpath => 'Tab/Scroll to Top', | ||
| 26 | keyname => 'Home', mod => ['control-mask'], | ||
| 27 | code => sub { $current_tab->scroll('top') }, | ||
| 28 | }, | ||
| 29 | 'Scroll to Bottom' => { | ||
| 30 | accelpath => 'Tab/Scroll to Bottom', | ||
| 31 | keyname => 'End', mod => ['control-mask'], | ||
| 32 | code => sub { $current_tab->scroll('bottom') }, | ||
| 33 | }, | ||
| 34 | 'Scroll Up' => { | ||
| 35 | accelpath => 'Tab/Scroll Up', | ||
| 36 | keyname => 'Page_Up', mod => [], | ||
| 37 | code => sub { $current_tab->scroll('up') }, | ||
| 38 | }, | ||
| 39 | 'Scroll Down' => { | ||
| 40 | accelpath => 'Tab/Scroll Down', | ||
| 41 | keyname => 'Page_Down', mod => [], | ||
| 42 | code => sub { $current_tab->scroll('down') }, | ||
| 43 | }, | ||
| 44 | ]; | ||
| 45 | } | ||
| 46 | |||
| 47 | sub new | ||
| 48 | { | ||
| 49 | my $class = shift; | ||
| 50 | my %args = @_; | ||
| 51 | |||
| 52 | my $object = $args{object}; | ||
| 53 | |||
| 54 | my $self = bless { | ||
| 55 | object => $object, | ||
| 56 | root => Gtk2::VBox->new(), | ||
| 57 | }, $class; | ||
| 58 | |||
| 59 | retain_future $object->call_method( | ||
| 60 | get_widget => () | ||
| 61 | )->on_done(sub { | ||
| 62 | $self->{root}->add( $self->build_widget( $_[0] ) ); | ||
| 63 | $self->{root}->show_all; | ||
| 64 | }); | ||
| 65 | |||
| 66 | return $self; | ||
| 67 | } | ||
| 68 | |||
| 69 | sub get_widget | ||
| 70 | { | ||
| 71 | my $self = shift; | ||
| 72 | return $self->{root}; | ||
| 73 | } | ||
| 74 | |||
| 75 | sub build_widget | ||
| 76 | { | ||
| 77 | my $self = shift; | ||
| 78 | my ( $obj ) = @_; | ||
| 79 | |||
| 80 | foreach my $type ( widgets ) { | ||
| 81 | next unless $obj->proxy_isa( "Circle.Widget." . $type->type ); | ||
| 82 | return $type->build( $obj, $self ); | ||
| 83 | } | ||
| 84 | |||
| 85 | die "Cannot build widget for $obj as I don't recognise its type\n"; | ||
| 86 | } | ||
| 87 | |||
| 88 | sub get_font | ||
| 89 | { | ||
| 90 | my $self = shift; | ||
| 91 | return Gtk2::Pango::FontDescription->from_string( $self->get_theme_var( "font" ) ); | ||
| 92 | } | ||
| 93 | |||
| 94 | sub get_label | ||
| 95 | { | ||
| 96 | my $self = shift; | ||
| 97 | |||
| 98 | return $self->{label} if defined $self->{label}; | ||
| 99 | |||
| 100 | my $label = $self->{label} = Gtk2::Label->new(""); | ||
| 101 | |||
| 102 | my $object = $self->{object}; | ||
| 103 | retain_future $object->watch_property_with_initial( | ||
| 104 | "level", | ||
| 105 | on_set => sub { | ||
| 106 | my ( $level ) = @_; | ||
| 107 | $label->modify_fg( $_ => $self->get_theme_colour( "level$level" ) ) for qw( normal active ); | ||
| 108 | }, | ||
| 109 | ); | ||
| 110 | |||
| 111 | return $label; | ||
| 112 | } | ||
| 113 | |||
| 114 | sub set_label_text | ||
| 115 | { | ||
| 116 | my $self = shift; | ||
| 117 | my ( $text ) = @_; | ||
| 118 | |||
| 119 | $self->get_label->set_text( $text ); | ||
| 120 | } | ||
| 121 | |||
| 122 | sub activated | ||
| 123 | { | ||
| 124 | my $self = shift; | ||
| 125 | |||
| 126 | my $object = $self->{object}; | ||
| 127 | |||
| 128 | if( $object->prop("level") > 0 ) { | ||
| 129 | retain_future $object->call_method( | ||
| 130 | reset_level => () | ||
| 131 | ); | ||
| 132 | } | ||
| 133 | } | ||
| 134 | |||
| 135 | sub scroll | ||
| 136 | { | ||
| 137 | my $self = shift; | ||
| 138 | $self->{scroller}->scroll( @_ ) if $self->{scroller}; | ||
| 139 | } | ||
| 140 | |||
| 141 | # Now read the theme | ||
| 142 | my %theme_vars; | ||
| 143 | |||
| 144 | { | ||
| 145 | my $theme_filename; | ||
| 146 | |||
| 147 | foreach ( $ENV{CIRCLE_FE_GTK_THEME}, | ||
| 148 | "$ENV{HOME}/.circle-fe-gtk.theme", | ||
| 149 | dist_file( "circle-fe-gtk", "circle-fe-gtk.theme" ) ) { | ||
| 150 | defined $_ or next; | ||
| 151 | -e $_ or next; | ||
| 152 | |||
| 153 | $theme_filename = $_; | ||
| 154 | last; | ||
| 155 | } | ||
| 156 | |||
| 157 | defined $theme_filename or die "Cannot find a circle-fe-gtk.theme"; | ||
| 158 | |||
| 159 | open( my $themefh, "<", $theme_filename ) or die "Cannot read $theme_filename - $!"; | ||
| 160 | |||
| 161 | while( <$themefh> ) { | ||
| 162 | m/^\s*#/ and next; # skip comments | ||
| 163 | m/^\s*$/ and next; # skip blanks | ||
| 164 | |||
| 165 | m/^(\S*)=(.*)$/ and $theme_vars{$1} = $2, next; | ||
| 166 | print STDERR "Unrecognised theme line: $_"; | ||
| 167 | } | ||
| 168 | } | ||
| 169 | |||
| 170 | sub get_theme_var | ||
| 171 | { | ||
| 172 | my $class = shift; | ||
| 173 | my ( $varname ) = @_; | ||
| 174 | return $theme_vars{$varname} if exists $theme_vars{$varname}; | ||
| 175 | print STDERR "No such theme variable $varname\n"; | ||
| 176 | return undef; | ||
| 177 | } | ||
| 178 | |||
| 179 | sub translate_theme_colour | ||
| 180 | { | ||
| 181 | my $class = shift; | ||
| 182 | my ( $colourname ) = @_; | ||
| 183 | |||
| 184 | return $colourname if $colourname =~ m/^#/; # Literal #rrggbb | ||
| 185 | return $theme_vars{$colourname} if exists $theme_vars{$colourname}; # hope | ||
| 186 | print STDERR "No such theme colour $colourname\n"; | ||
| 187 | return undef; | ||
| 188 | } | ||
| 189 | |||
| 190 | sub get_theme_colour | ||
| 191 | { | ||
| 192 | my $class = shift; | ||
| 193 | my ( $varname ) = @_; | ||
| 194 | return Gtk2::Gdk::Color->parse( $theme_vars{$varname} ) if exists $theme_vars{$varname}; | ||
| 195 | print STDERR "No such theme variable $varname for a colour\n"; | ||
| 196 | return undef; | ||
| 197 | } | ||
| 198 | |||
| 199 | 1; |