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;
}