PHP-GTK2 Newsletter

PHP-GTK2 Tips & Techniques
FREE Newsletter
by kksou



Sample Code 190: How to change the font and color of checkbox labels?
Written by kksou   
Saturday, 03 March 2007
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 checkbox labels?


Solution

Sample Code
1   
2   
3   
4   
5   
6   
7   
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   
35   
36   
37   
38   
39   
40   
41   
<?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 checkbox 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
$checkbox1 = setup_checkbox('checkbox 1');
$checkbox2 = setup_checkbox('checkbox 2', "Arial Bold 16");
$checkbox3 = setup_checkbox('checkbox 3', "Times New Roman Italic 20", "#0000ff");

// pack them inside vbox
$vbox->pack_start($checkbox1, 0, 0);
$vbox->pack_start($checkbox2, 0, 0);
$vbox->pack_start($checkbox3, 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_checkbox($label, $fg='', $color='') {
    $checkbox = new GtkCheckButton($label);
    $checkbox->connect('toggled', "on_toggle");
    $button_label = $checkbox->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
    }
  • Note that this is only 70% of the sample code. You have to be a registered member to see the entire sample code. Please login or register.
  • Registration is free and immediate.
  • Have some doubt about the registration? Please read this forum article.
Explanation

The above example make use of the code in How to setup and process checkboxes?

What's new here:

  1. Get the button label.
  2. Set the font and font size of the button label.
  3. Set the color of the button label.

Related Links

User reviews

There are no user reviews yet.

Note: You have to be a registered member to leave a comment. Free registration here.

 
< Prev   Next >

Blog - Forum - Privacy Policy - Contact Us
Copyright © 2006-2008. kksou.com. All Rights Reserved