Re: Notify signal font size has changed

Am 09.09.2014 22:28, schrieb Torsten Schoenfeld:
On 08.09.2014 22:20, Torsten Schoenfeld wrote:
In principle, GtkSettings' "notify::gtk-font-name" can be used for this,
but it's not available in perl-Gtk2, only perl-Gtk3:

perl -MGtk3=-init -E'my $s = Gtk3::Settings::get_default (); say $s->get
("gtk-font-name"); $s->signal_connect ("notify::gtk-font-name", sub {
say $s->get ("gtk-font-name") }); Gtk3::main ();'

That was not entirely correct, it turns out.  While
gtk_settings_get_default is not bound in perl-Gtk2 (patches welcome),
gtk_widget_get_settings is.  So try this:

perl -MGtk2=-init -E'my $w = Gtk2::Window->new; my $s =
$w->get_settings; say $s->get ("gtk-font-name"); $s->signal_connect
("notify::gtk-font-name", sub { say $s->get ("gtk-font-name"); });
Gtk2->main ();'

To parse the font name, you can use Pango::FontDescription->from_string.

Thanks Thorsten! 
Unfortunately your code doesn't work on my system (Perl 5.20. gtk2-perl 2.24).

But after some searches I found a signal which works for me \O/ 
Here's my test code:

#!/usr/bin/perl -w

use strict;

use Gtk2 '-init';
our $last_fsize;

sub changed {
    my $window = shift;
    my $fontsize = get_fontsize($window);
    if ($last_fsize != $fontsize) {
        print "changed!\n";
        # update it
        $last_fsize = $fontsize;

sub get_fontsize {
    my $window = shift;
    my $context = $window->get_pango_context();
    my $fontDesc = $context->get_font_description();
    my $fontsize = $fontDesc->get_size();
        my $pango_scale = Gtk2::Pango->scale();
    if ($fontsize > 200) {
        $fontsize = $fontsize/$pango_scale ;
    return $fontsize;

my $window = Gtk2::Window->new('toplevel');
$window->signal_connect(destroy => sub { Gtk2->main_quit; });
$window->signal_connect('style-set' => \&changed, $window);

$last_fsize = get_fontsize($window);



I found the hint about 'style-set' in a chrome bug report
where're problems with wrong font size in menus against
the set font size in Gtk2.

If anybody interested to read:

The interesting part is the function void Gtk2UI::Initialize() in a git commit:

Thanks again for your help :-)


"Two things are infinite: the universe and human stupidity; and I'm not sure about the the universe."   --   
Albert Einstein

[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]