508. How to change the font and color of radio button labels?

Problem

You would like to change the font and set the color of the labels of GtkCheckButtons as shown below:

How to change the font and color of radio button labels?


Solution


Sample Code

1   
2   
3   
4   
5   
6   
8   
9   
10   
11   
12   
13   
14   
15   
16   
17   
18   
19   
20   
21   
22   
23   
24   
25   
26   
27   
28   
29   
30   
31   
32   
33   
34   
36   
37   
38   
39   
40   
41   
42   
43   
44   
45   
46   
47   
48   
49   
50   
51   
52   
53   
54   
<?php
$window = new GtkWindow();
$window->set_size_request(400, 240);
$window->connect_simple('destroy', array('Gtk','main_quit'));
$window->add($vbox = new GtkVBox());


// display title
$title = new GtkLabel("Change the font and set the color of radio button labels");
$title->modify_font(new PangoFontDescription("Times New Roman Italic 10"));
$title->modify_fg(Gtk::STATE_NORMAL, GdkColor::parse("#0000ff"));
$title->set_size_request(-1, 40);
$vbox->pack_start($title, 0, 0);

// setup checkboxes
$radio0 = setup_radio(null, 'option 1');
$radio1 = setup_radio($radio0, 'option 2', "Arial Bold 16");
$radio2 = setup_radio($radio0, 'option 3', "Times New Roman Italic 20", "#0000ff");

// pack them inside vbox
$vbox->pack_start($radio0, 0, 0);
$vbox->pack_start($radio1, 0, 0);
$vbox->pack_start($radio2, 0, 0);

// add a status area
$vbox->pack_start($status_area = new GtkLabel('Select the checkboxes'));

$window->show_all();
Gtk::main();

// function to simplify the display of grouped radio buttons
function setup_radio($radio_grp, $label, $fg='', $color='') {
    $radio = new GtkRadioButton($radio_grp, $label);
    $radio->connect('toggled', "on_toggle");
    $button_label = $radio->child; // note 1
    if ($fg!='')
        $button_label->modify_font(new PangoFontDescription($fg)); // note 2
    if ($color!='') {
        $button_label->modify_fg(Gtk::STATE_NORMAL, GdkColor::parse($color)); // note 3
        $button_label->modify_fg(Gtk::STATE_PRELIGHT, GdkColor::parse($color)); // note 3
        $button_label->modify_fg(Gtk::STATE_ACTIVE, GdkColor::parse($color)); // note 3
    }
    return $radio;
}

// call-back function when user pressed a radio button
function on_toggle($radio) {
    global $status_area;
    $label = $radio->child->get_label();
    $status_area->set_text("selected radio button: $label");
}
?>

Output

As shown above.

 

Add comment


Security code
Refresh