How do you modify font size in a running Gtk3 app? - perl

I'm trying to find a way(the correct way) to modify the font size in a running Gtk3 Perl app using the Ctrl key plus mouse wheel. I can use the code below to modify the font-size but do you really have to use providers and styles to achieve this?
#! /usr/bin/env perl
use 5.26.1;
use local::lib;
use warnings;
use strict;
use utf8;
use constant MAX_FONT_SIZE => 200;
use constant MIN_FONT_SIZE => 12;
use Glib qw(TRUE FALSE);
use Gtk3 qw(init);
my $str = "label {font-size: ".MIN_FONT_SIZE."px;}";
sub getWheel {
my ($object, $event, $sp) = #_;
my ($ctrl, $mod) = #{$event->state};
my ($style, $provider) = #{$sp};
state $font_size = MIN_FONT_SIZE;
if ($ctrl eq q<control-mask> && $mod eq q<mod2-mask>) {
if ($event->direction eq q<up>) {
if ($font_size < MAX_FONT_SIZE) {
$font_size += 4;
$str = "label {font-size: ${font_size}px;}";
$provider->load_from_data ($str, length($str));
$style->add_provider($provider, 600);
}
}elsif ($event->direction eq q<down>){
if ($font_size > MIN_FONT_SIZE) {
$font_size -= 4;
$str = "label {font-size: ${font_size}px;}";
$provider->load_from_data ($str, length($str));
$style->add_provider($provider, 600);
}
}
}
FALSE;
}
my $window = Gtk3::Window->new(q<toplevel>);
my $label = Gtk3::Label->new(q<Hello>);
my $provider = Gtk3::CssProvider->new();
$provider->load_from_data ($str, length($str));
my $style = $label->get_style_context();
$style->add_provider($provider, 600);
$window->add_events(q<GDK_SCROLL_MASK>);
$window->signal_connect(delete_event => sub{Gtk3->main_quit; FALSE});
$window->signal_connect(scroll_event => \&getWheel, [$style, $provider]);
$window->set_default_size(500, 300);
$window->add($label);
$window->show_all();
Gtk3->main;
Is there another way to modify the font-size of a running Gtk3 app?

You can also use pango_font_description_set_absolute_size(). For example:
use feature qw(say state);
use strict;
use warnings;
use constant MAX_FONT_SIZE => 200;
use constant MIN_FONT_SIZE => 12;
use Glib qw(TRUE FALSE);
use Gtk3 qw(init);
use Pango;
{
my $window = Gtk3::Window->new(q<toplevel>);
my $label = Gtk3::Label->new(q<Hello>);
set_label_font_size( $label, MIN_FONT_SIZE );
$window->add_events(q<GDK_SCROLL_MASK>);
$window->signal_connect(delete_event => sub{Gtk3->main_quit; FALSE});
$window->signal_connect(scroll_event => sub { get_wheel( $label, #_ ) } );
$window->set_default_size(500, 300);
$window->add($label);
$window->show_all();
Gtk3->main;
}
sub set_label_font_size {
my ( $label, $size ) = #_;
my $context = $label->get_pango_context();
my $font_description = $context->get_font_description();
$font_description->set_absolute_size($size * Pango::SCALE);
$context->set_font_description($font_description);
# A bug or am I missing somthing?? But for now I had to modify the label text in order
# for the fontsize to show up.
$label->set_text( $label->get_text() );
}
sub get_wheel {
my ($label, $widget, $event ) = #_;
my ($ctrl, $mod) = #{$event->state};
state $font_size = MIN_FONT_SIZE;
if ($ctrl eq q<control-mask> && $mod eq q<mod2-mask>) {
if ($event->direction eq q<up>) {
if ($font_size < MAX_FONT_SIZE) {
$font_size += 4;
}
}
elsif ($event->direction eq q<down>){
if ($font_size > MIN_FONT_SIZE) {
$font_size -= 4;
}
}
else {
return FALSE;
}
set_label_font_size( $label, $font_size );
}
return FALSE;
}

Related

Custom Storable hooks for dclone-ing a light-weight object referencing a heavy-weight object

Say I have a tiny object that has a reference to a huge object:
package Tiny;
sub new {
my ($class, $tiny, $large) = #_;
return bless { tiny => $tiny, large => $large };
}
I'd like to create a STORABLE_freeze/STORABLE_thaw pair that lets me (recursively) clone $tiny but maintain/keep the reference to $large as-is without cloning $large too.
I tried temporarily deleting $self->{large} (see below), and putting it in a hash with a Scalar::Util::refaddr key and a weak reference to $large, serializing the rest of $self, and then putting the (weak) reference back into both the original object immediately and the cloned one in STORABLE_thaw, but it is a mess, and on every clone, the weak ref value gets deleted when it goes out of scope, but the key remains in the hash forever leaking memory and I need a global class member hash (%largeWeakRefs) to hold the temporary $large reference. Has a smell.
How it that possible to do this in a cleaner way?
Here is my solution using the hash to hold the large ref temporarily:
package Tiny;
use Scalar::Util qw(refaddr weaken);
sub new {
my ( $class, $tiny, $large ) = #_;
return bless { tiny => $tiny, large => $large }, $class;
}
# Ugly temporary storage to hold $large refs from _freeze to _thaw...
my %largeWeakRefs;
sub STORABLE_freeze {
my ( $self, $cloning ) = #_;
my $large = delete local $self->{large};
my $refaddr = refaddr $large;
$largeWeakRefs{$refaddr} = $large;
weaken $largeWeakRefs{$refaddr};
my %restOfSelf = %$self;
$self->{large} = $large;
return $refaddr, \%restOfSelf;
}
sub STORABLE_thaw {
my ($self, $cloning, $refaddr, $restOfSelf) = #_;
%$self = %$restOfSelf;
$self->{large} = $largeWeakRefs{$refaddr};
return $self;
}
(Yes I know, my example only handles cloning, not straight-up freeze and thaw)
You could add reference counts.
my %larges;
sub STORABLE_freeze {
my ( $self, $cloning ) = #_;
if ($cloning) {
my $large_key = pack('j', refaddr(self->{large}));
$larges{$large_key} //= [ $self->{large}, 0 ];
++$larges{$large_key}[1];
return ( $large_key, $self->{tiny} );
} else {
return ( "", $self->{tiny}, $self->{large} );
}
}
sub STORABLE_thaw {
my ( $self, $cloning, $serialized ) = splice(#_, 0, 3);
if ($cloning) {
my $large_key = $serialized;
$self->{ tiny } = shift;
$self->{ large } = $larges{$large_key}[0];
--$larges{$large_key}[1]
or delete($larges{$large_key});
} else {
$self->{ tiny } = shift;
$self->{ large } = shift;
}
}
Untested.
If the cloning process dies, you'll have a memory leak.
Alternatively, you could avoid the need for external resources as follows:
use Inline C => <<'__EOS__';
IV get_numeric_ref(SV *sv) {
SvGETMAGIC(sv);
if (!SvROK(sv))
croak("Argument not a reference");
sv = MUTABLE_SV(SvRV(sv));
SvREFCNT_inc(sv);
return PTR2IV(sv); /* Despite its name, can be used to convert pointer to IV */
}
SV* get_perl_ref_from_numeric_ref(IV iv) {
SV* sv = PTR2IV(iv);
return newRV_noinc(sv);
}
__EOS__
sub STORABLE_freeze {
my ( $self, $cloning ) = #_;
if ($cloning) {
return ( pack('j', get_numeric_ref($self->{large})), $self->{tiny} );
} else {
return ( "", $self->{tiny}, $self->{large} );
}
}
sub STORABLE_thaw {
my ( $self, $cloning, $serialized ) = splice(#_, 0, 3);
if ($cloning) {
$self->{ tiny } = shift;
$self->{ large } = get_perl_ref_from_numeric_ref(unpack('j', $serialized));
} else {
$self->{ tiny } = shift;
$self->{ large } = shift;
}
}
Didn't test STORABLE_freeze and STORABLE_thaw, but tested the C/XS code using the following:
use strict;
use warnings;
use feature qw( say state );
use Cpanel::JSON::XS qw( );
sub _dump {
state $encoder = Cpanel::JSON::XS->new->canonical->allow_nonref;
return $encoder->encode($_[0]);
}
{
my %h = ( a => 4, b => 5 );
say _dump(\%h); # {"a":4,"b":5}
say sprintf "0x%x", \%h; # 0x32cdbf8
say Internals::SvREFCNT(%h); # 1
my $i = get_numeric_ref(\%h);
say sprintf "0x%x", $i; # 0x32cdbf8
say Internals::SvREFCNT(%h); # 2
my $ref = get_perl_ref_from_numeric_ref($i);
say sprintf "0x%x", $ref; # 0x32cdbf8
say Internals::SvREFCNT(%h); # 2
say _dump($ref); # {"a":4,"b":5}
}
If the cloning process dies, you'll have a memory leak. I suppose it would be safe to rely on "large" not going anywhere during the cloning process, so you could remove the SvREFCNT_inc and change newRV_noinc to newRV to avoid the potential memory leak.
To avoid the possible memory leak, never store "large" in the object.
my %larges;
sub new {
my $class = shift;
my $self = bless({}, $class);
return $self->_init(#_);
}
sub _init {
my ($self, $tiny, $large) = #_;
$self->{ tiny } = $tiny;
{
my $large_key = pack('j', refaddr($self));
$self->{ large_key } = $large_key;
$larges{ $large_key } = $large;
}
return $self;
}
sub DESTROY {
my ($self) = #_;
if (defined( my $large_key = $self->{ large_key } )) {
delete( $larges{ $large_key } );
}
}
sub STORABLE_freeze {
my ( $self, $cloning ) = #_;
if ($cloning) {
return ( $self->{large_key}, $self->{tiny} );
} else {
return ( "", $self->{tiny}, $larges{ $self->{large_key} } );
}
}
sub STORABLE_thaw {
my ( $self, $cloning, $serialized ) = splice(#_, 0, 3);
if ($cloning) {
my ($tiny) = #_;
my $large_key = $serialized;
$self->_init($tiny, $larges{ $large_key });
} else {
$self->_init(#_);
}
}
Untested.
No memory leaks if the cloning process dies.

Module to manage HTTP errors

I'm writing a Perl module Result to manage HTTP errors.
I am a little lost to generate the function managing all this.
Result.pm
package Result;
use strict;
use warnings;
use Data::Dumper;
use Log::Log4perl;
sub new
{
my $class = shift;
my %params = #_;
my $self = {
code => $params{'code'},
value => $params{'value'}
};
bless $self, $class;
return $self;
}
sub generateError
{
my $self = shift;
my %params = #_;
if($self->{'code'} == 200)
{
return "Your request was well executed !";
}
elsif($self->{'code'} == 400)
{
return "There is an error in your request! Please verify it!";
}
elsif($self->{'code'} == 404)
{
return "We did not find for what you ask !"
}
elsif($self->{'code'} == 500)
{
return "Internal error of the server, please re-try later !";
}
elsif($self->{'code'} == 504)
{
return "Your request has set of time to be executed, please retry !";
}
}
1;
App.pm
sub template
{
my $self = shift;
my $query = $self->query;
my $id = $query->param('id');
my $session = $self->param('session');
my $profile = $session->param('profile');
my $Project = Project->newFromId($id);
if(!$Project or $Project eq 'NOT_FOUND')
{
return $self->redirect('?rm=notfound');
}
if(!$profile->{'uid'} or $Project->{'userId'} != $profile->{'uid'})
{
return $self->redirect('?rm=notfound');
}
my $mailContent = from_json($Project->{'mail'});
my $templateContent = formatTemplate($Project->{'template'});
my $infos = [
{
'ID' => $id,
'TEMPLATE' => $templateContent,
'MAILSUBJECT' => $mailContent->{'subject'},
'MAILBODY' => $mailContent->{'body'}
}
];
if($mailContent->{'type'} eq 'text')
{
$infos->[0]{'MAILTEXT'} = 1;
}
else
{
$infos->[0]{'MAILHTML'} = 1;
}
return $self->processtmpl('template.tmpl', $infos);
}
1;

perl script with AnyEvent is not going fast enough

Here is the little script I created to do HTTP transactions from a file with a list of URLs. The problem is that it's not able to do HTTP transactions as fast as I would like. More precisely, I set the rate to be 200/seconds, but it was able to send only at about 50/second. The server is powerful enough to handle 100/second.
This was run on a powerful PC with E5-1650 CPU and 64GB of RAM running Ubuntu 14.04 desktop. When the script runs, the CPU usage is only about 12%. The command I used was perl httpStresser.pl urlList rate 200.
Any idea why?
use AnyEvent;
use EV;
use AnyEvent::HTTP;
use AnyEvent::Handle;
use Time::HiRes qw( gettimeofday );
my $expectedRespCode = 200;
my $rate = 1;
my #urls = ();
readUrls(shift);
my $numOfUrls = $#urls;
my $start = time();
my $printed = 0; #have we printed the completion msg.
my $gId = 0;
my $spawned;
my #ctx = ();
my $i;
for ($i=0; $i<=$#ARGV; $i++) {
if ($ARGV[$i] =~ /^expect/) {
$expectedRespCode = $ARGV[$i+1];
$i++;
} elsif ($ARGV[$i] =~ /^rate/) {
$rate = $ARGV[$i+1];
print "rate is now $rate\n";
$i++;
} elsif ($ARGV[$i] =~ /^skip/) {
$gId = $ARGV[$i+1];
$i++;
} else {
die "only max, stayup are supported\n";
}
}
my $spawned = 0;
my $w = AnyEvent->condvar;
$| = 1;
my $start = getTS();
my $_timer;
$_timer = AnyEvent->timer(after => 0, interval => 0.001, cb => ::timeoutHandler);
$w->recv;
sub kickoff {
my $id = $gId ++;
if ($id > $numOfUrls) {
if ($printed == 0) {
print "done!!\n"; $printed = 1;
}
return;
}
#print "$id\n";
http_get $urls[$id], headers => { }, sub {
my $statusCode = $_[1]->{Status};
#printf "status $statusCode %d\n", time() - $start;
if (($id % 100) == 0) {
print "$id\n";
}
if ($statusCode != $expectedRespCode) {
print "unexpected resp code $id:$statusCode $urls[$id]\n";
}
};
}
sub timeoutHandler {
#print time(), "|\n";
if (! defined $start) {
$start = getTS(); kickoff(); $spawned = 1; return;
}
my $delta = getTS() - $start;
my $target = $delta * $rate;
#printf "%.4f %4d $spawned\n", $delta, $target;
for (; $spawned <= $target; $spawned++) {
kickoff();
}
if ($delta >= 1.0 ) {
$start += 1.0; $spawned = 0;
}
}
sub readUrls {
my $fname = shift;
my $line;
open FD, $fname || die "Failed to open $fname $!\n";
while (<FD>) {
chomp($line = $_);
push #urls, $line;
}
close FD;
}
sub getTS {
my ($seconds, $microseconds) = gettimeofday;
return $seconds + (0.0+ $microseconds)/1000000.0;
}

Can't build perl class use autoload

class Gene
package Gene;
use strict;
use Carp;
use vars qw($AUTOLOAD);
my %_ok_gene_attr = (
"id" => "string",
"name" => "string",
"chrom" => "string", # chromosome or seq id
"txtStart" => "int", # 1-based
"txtEnd" => "int" # 1-based
);
sub new {
my ($class, %arg) = #_;
my $self = {};
$self->{_id} = $arg{id} || croak "no id";
$self->{_name} = $arg{name} || croak "no db";
$self->{_chrom} = $arg{chrom} || croak "no seq_id";
$self->{_strand} = $arg{strand} || -1;
$self->{_txStart} = $arg{txStart} || -1;
$self->{_txEnd} = $arg{txEnd} || -1;
$self->{_cdsStart} = $arg{cdsStart} || -1;
$self->{_cdsEnd} = $arg{cdsEnd} || -1;
$self->{_exonCount} = $arg{exonCount} || -1;
$self->{_exonStarts} = $arg{exonStarts} || -1;
$self->{_exonEnds} = $arg{exonEnds} || -1;
$self->{_score} = $arg{score} || -1;
$self->{_name2} = $arg{name2} || -1;
$self->{_cdsStartStat} = $arg{cdsStartStat} || -1;
$self->{_cdsEndStat} = $arg{cdsEndStat} || -1;
$self->{_exonFrames} = $arg{exonFrames} || -1;
bless($self, $class);
}
sub AUTOLOAD {
my ($self, $newvalue) = #_;
my ($operation, $attribute) = ($AUTOLOAD =~ /(get)(_\w+)$/);
unless ($operation && $attribute) {
croak "Method name $AUTOLOAD is not in the recognized form get_attribute\n";
}
unless (defined $_ok_gene_attr{$attribute}) {
croak "No such attribute '$attribute' exists in the class ", ref($self);
}
if ($operation eq 'get') {
*{$AUTOLOAD} = sub { shift->{$attribute} };
} elsif($operation eq 'set') {
*{$AUTOLOAD} = sub { shift->{$attribute} = shift };
$self->{$attribute} = $newvalue;
}
return $self->{$attribute};
}
sub DESTROY {
my($self) = #_;
$self->_decr_count( );
}
I use this class in
open my $in, '<', q/C:\Users\Jesse\Desktop\refGene.txt/ or die "Cannot open file : $!";
while(<$in>) {
chomp $_;
my #temp= split(/\t/, $_);
my $i=0;
my $temp1= Gene->new("id" => $temp[0],"name"=>$temp[1],"chrom"=>$temp[2], "strand"=>$temp[3],"txStart"=>$temp[4], "txEnd"=>$temp[5],"cdsStart"=>$temp[6],"cdsEnd"=>$temp[7],"exonCount"=>$temp[8],"exonStarts"=>$temp[9],"exonEnds"=>$temp[10],"score"=>$temp[11],"name2"=>$temp[12],"cdsStartStat"=>$temp[13],"cdsEndStat"=>$temp[14],"exonFrames"=>$temp[15]);
}
is has a error
(in cleanup) Method name Gene::_decr_count is not in the recognized form get_attribute
It also seems the autoload function doesn't work when i use $temp1->set_id("1234")?
Try this some kind of fixed version of Gene.pm:
package Gene;
use strict;
use Carp;
use vars qw($AUTOLOAD);
use Data::Dumper;
my %_ok_gene_attr = (
"_id" => "string",
"_name" => "string",
"_chrom" => "string", # chromosome or seq id
"_txtStart" => "int", # 1-based
"_txtEnd" => "int" # 1-based
);
sub new {
my ($class, %arg) = #_;
my $self = {};
$self->{_id} = $arg{id} || croak "no id";
$self->{_name} = $arg{name} || croak "no db";
$self->{_chrom} = $arg{chrom} || croak "no seq_id";
$self->{_strand} = $arg{strand} || -1;
$self->{_txStart} = $arg{txStart} || -1;
$self->{_txEnd} = $arg{txEnd} || -1;
$self->{_cdsStart} = $arg{cdsStart} || -1;
$self->{_cdsEnd} = $arg{cdsEnd} || -1;
$self->{_exonCount} = $arg{exonCount} || -1;
$self->{_exonStarts} = $arg{exonStarts} || -1;
$self->{_exonEnds} = $arg{exonEnds} || -1;
$self->{_score} = $arg{score} || -1;
$self->{_name2} = $arg{name2} || -1;
$self->{_cdsStartStat} = $arg{cdsStartStat} || -1;
$self->{_cdsEndStat} = $arg{cdsEndStat} || -1;
$self->{_exonFrames} = $arg{exonFrames} || -1;
bless($self, $class);
}
sub AUTOLOAD {
my ($self, $newvalue) = #_;
my ($operation, $attribute) = ($AUTOLOAD =~ /(get|set)(_\w+)$/);
unless ($operation && $attribute) {
croak "Method name $AUTOLOAD is not in the recognized form set_{attribute} or get_{attribute}\n";
}
unless (defined $_ok_gene_attr{$attribute}) {
croak "No such attribute '$attribute' exists in the class ", ref($self);
}
if ($operation eq 'get') {
no strict 'refs';
*{$AUTOLOAD} = sub { shift->{$attribute} };
} elsif($operation eq 'set') {
no strict 'refs';
*{$AUTOLOAD} = sub { shift->{$attribute} = shift };
$self->{$attribute} = $newvalue;
}
return $self->{$attribute};
}
sub DESTROY {
my($self) = #_;
$self->_decr_count( );
}
Tested with the following script:
#!/usr/bin/perl
use strict;
use warnings;
use Gene;
my $t1 = Gene->new(id=>"id", name=>"name", chrom=>"chrom");
$t1->set_id("1234");
print $t1->get_id(), "\n";
Beside using Perl's builtin OO system, you may also want to take a look of some other OO system, such as Moose. See perlootut for further details.

Safe way to establish the initial state again.

Is there a possibility of calling the function main without the function _end_win being called?
#!/usr/bin/env perl
use warnings;
use strict;
use 5.10.0;
use Term::ReadKey;
use constant {
NEXT_getch => -1,
CONTROL_C => 0x03,
CONTROL_D => 0x04,
KEY_ENTER => 0x0d,
KEY_RIGHT => 0x1b5b43,
KEY_LEFT => 0x1b5b44,
};
say main();
sub main {
my $arg = {};
$arg->{handle_out} = *STDOUT;
_init_scr( $arg );
while ( 1 ) {
my $c = _getch( $arg );
if ( ! defined $c ) {
_end_win( $arg );
warn "EOT";
return;
}
next if $c == NEXT_getch;
given ( $c ) {
when ( $c >= 97 && $c <= 122 ) {
print chr $c;
$arg->{string} .= chr $c;
}
when ( $c == KEY_RIGHT ) {
print '>';
$arg->{string} .= '>';
}
when ( $c == KEY_LEFT ) {
print '<';
$arg->{string} .= '<';
}
when ( $c == CONTROL_D ) {
_end_win( $arg );
return;
}
when ( $c == CONTROL_C ) {
_end_win( $arg );
print STDERR "^C";
kill( 'INT', $$ );
return;
}
when ( $c == KEY_ENTER ) {
_end_win( $arg );
return $arg->{string};
}
}
}
}
sub _init_scr {
my ( $arg ) = #_;
$arg->{old_handle} = select( $arg->{handle_out} );
$arg->{backup_flush} = $|;
$| = 1;
Term::ReadKey::ReadMode 'ultra-raw';
}
sub _end_win {
my ( $arg ) = #_;
print "\n\r";
Term::ReadKey::ReadMode 'restore';
$| = $arg->{backup_flush};
select( $arg->{old_handle} );
}
sub _getch {
my ( $arg ) = #_;
my $c1 = ReadKey 0;
return if ! defined $c1;
if ( $c1 eq "\e" ) {
my $c2 = ReadKey 0.10;
if ( ! defined $c2 ) { return NEXT_getch; }
elsif ( $c2 eq 'C' ) { return KEY_RIGHT; }
elsif ( $c2 eq 'D' ) { return KEY_LEFT; }
elsif ( $c2 eq '[' ) {
my $c3 = ReadKey 0;
if ( $c3 eq 'C' ) { return KEY_RIGHT; }
elsif ( $c3 eq 'D' ) { return KEY_LEFT; }
else {
return NEXT_getch;
}
}
else {
return NEXT_getch;
}
}
else {
return ord $c1;
}
}
To ensure that the terminal is reset when your program exits, put the reset code into an END block. For example, you could replace your _end_win sub with:
END {
print "\n\r";
Term::ReadKey::ReadMode 'restore';
}
(I removed the lines resetting $| and the selected output filehandle since the process is exiting anyhow, so they're about to become irrelevant.)
An END block will always run when the program terminates in a "normal" way, such as calling exit or die or hitting the end of the executable code. It does not fire when the process terminates due to receiving a signal; it looks like you're handling the ctrl-C character directly, but you may want to consider adding a %SIG{INT} handler as well, in case someone sends you a kill -2.