Re: Setting Font Faces and Sizes



On Fri, 05 Jan 2007 14:39:41 -0600
Nik Ogura <nogura uma-mn com> wrote:

I apologize in advance for this question, I'm a bit embarrassed to have
to ask it.  I've been running in circles looking for the answer though,
so here it goes:

Can someone give me an example of how to set a) the font face and/ or b)
the font size for a Label or Entry Widget?

Just as important, where should I be looking for this sort of thing?

Usually it's set in the style in ~.gtkrc-2.0  You can download prebuilt
styles, just google for them. The best thing to do is search thru the 
maillist archives for examples, and look at the demo programs that
come with the modules and see how they do it.

It can all get very tricky, depending on the widget and default style, 
because you may need to explicitly unset the default style, before 
the new style can be set. 

Read "perldoc Gtk2::Style", for the details, but each widget seems
to need slightly different handling.... it's not as easy as in Tk. :-)

But here is a basic demo for the Label, which contains most
of the basics.

#!/usr/bin/perl
use warnings;
use strict;
use Glib qw/TRUE FALSE/;
use Gtk2 '-init';

#you can set the fg color of a label's text, but not the bg
#you can put the label in an eventbox, and color the bg
#of the eventbox

my $greyl = Gtk2::Gdk::Color->new (0x9999,0x9999,0x9999);
my $bluel = Gtk2::Gdk::Color->new (0,0xCCCC,0xFFFF);
my $red = Gtk2::Gdk::Color->new (0xFFFF,0,0);
my $white = Gtk2::Gdk::Color->new (0xFFFF,0xFFFF,0xFFFF);

my $window = Gtk2::Window->new('toplevel');
$window->signal_connect( destroy => sub { Gtk2->main_quit; } );

$window->set_title("Label");
my $vbox = Gtk2::VBox->new( FALSE, 5 );
$window->add($vbox);
$window->set_border_width(5);
$window->set_size_request(300,200);
$window->modify_bg('normal',$greyl);

my $frame = Gtk2::Frame->new("Colored Label");
my $label = Gtk2::Label->new("This is a  Colored Label");
$label->modify_fg('normal', $white);

$frame->modify_bg('normal', $red);

$vbox->pack_start( $frame, FALSE, FALSE, 0 );

#used for coloring background
my $coleventb0 = Gtk2::EventBox->new();
$coleventb0->modify_bg ('normal', $bluel);
$coleventb0->set_border_width(2);

$frame->add($coleventb0);
$coleventb0->add($label);

my $fg = 'white',
my $bg = 'black';

my $label_pango = Gtk2::Label->new();
$label_pango->set_markup("<span foreground=\"$fg\" background=\"$bg\" size=\"30000\"><b>Test 
Text</b></span>");
$vbox->pack_end( $label_pango, FALSE, FALSE, 0 );

$window->show_all;

# color list default
open COLORS, "./rgb.txt"
  or die "Could not find the rgb.txt file under /usr/X11R6/lib/X11/ ($!)";
        
foreach my $color (<COLORS>){
        chomp $color;
        if (!($color =~ m/^!/)){
                $color =~ s/.+\t\t//;
                print $color."\n";
        }
    }   
Gtk2->main;
__END__

#########################################################
########################################################3
and here is one for the Entry

#!/usr/bin/perl
use warnings;
use strict;
use Gtk2 '-init';

#the Entry settings in ~/.gtkrc-2.0 dosn't
#seem to do anything except create a subtle outline 
#under the entry box. So you need to manually make one. 

Gtk2::Rc->parse_string(<<__);

include "/usr/local/share/themes/Bumblebee/gtk-2.0/gtkrc"
style "normal" {
  font_name ="serif 30"
}

style "my_entry" {
  font_name ="sans 25"
  text[NORMAL] = "#FF0000"
}

widget "*" style "normal"
widget "*Entry*" style "my_entry"
__

my $window = Gtk2::Window->new;
$window->set_title("Hello world");
$window->signal_connect( destroy => sub { Gtk2->main_quit; } );

my $vbox = Gtk2::VBox->new();
$vbox->set( "border_width" => 10 );
$window->add($vbox);

my $label = Gtk2::Label->new("ÃÃÃÃÃÃ");
$vbox->pack_start( $label, 0, 0, 5 );    # expand?, fill?, padding

my $entry = Gtk2::Entry->new();
$vbox->pack_start( $entry, 0, 0, 5 );

my $button = Gtk2::Widget->new( "Gtk2::Button", label => "Quit" );
$button->signal_connect( clicked => sub { Gtk2->main_quit; } );

$vbox->pack_start( $button, 0, 0, 5 );

$window->show_all();

Gtk2->main;
__END__

Goodluck, it's a PITA, so try to collect alot of examples. :-)

zentara

-- 
I'm not really a human, but I play one on earth.
http://zentara.net/japh.html



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