Chapter 6. Gtk2::Label

Objective

Labels are an integral part of any GUI. Gtk2::Label is (normally) one of Gtk+'s "no-window" widgets, which means it has no Gtk2::Gdk::Window and therefore cannot receive input events. To get mouse clicks on a label, pack the label into a Gtk2::EventBox, and connect to the appropriate event signals on the Gtk2::EventBox widget, instead. Gtk2::Label has a few nice features that we will explore during this lesson.

Table 6-1. Gtk2 object classes used

Gtk2 Class Name
Gtk2::Window
Gtk2::VBox
Gtk2::HSeparator
Gtk2::Label
Gtk2::Entry
Gtk2::Table

Table 6-2. Special Class used

Gtk2 Class Name
Glib::MainLoop

General discussion on Gtk2::Label

Note

Gtk2::Label is a subclass of Gtk2::Misc.

Moving text within the label

Gtk2::Misc has one method that comes in handy when using Gtk2::Label. This is the $misc->set_alignment ($xalign, $yalign). We use it to move the text around within the space allocated to the label. This is handy when we have an instance of Gtk2::Label packed into an instance of Gtk2::Table. $xalign and $yalign should be between 0 and 1. Thus to have the text in the middle, you give it a value of 0.5. To move it to the right, you give $xalign a value of 1 and to move it to the left, give it a value of 0.

Setting the angle of the text

Warning

This feature requires gtk+ >= 2.6.0.

The text in the label can sit at a certain angle. The default is 0 degrees. You set the angle by the following method:
	$label->set_angle ($angle) 
	
In our sample program, we will utilize Glib::MainLoop's Glib::Timeout method that will keep rotating one of the labels (by using $label->set_angle ($angle) ) at a specified interval.

Setting the text, a word on Pango markup and mnemonic

The value of the label is set by using $label->set_text ($str). This will give only a standard label, without any formatted text. If you want to format the label's text, there is the $label->set_markup ($str) method. This method takes either normal text, or Pango markup. If Pango markup is a new word, please check the Appendix A for more detail on Pango.

Mnemonic

In the days before the mouse, the "alt + some key" had a special trigger function. "alt +f" for instance would open the file menu. The file menu will indicate the key to use with alt by showing that character underlined.

$label->set_text_with_mnemonic ($str) will cause the label to treat the underscore as a special character. This will create the "alt + some key" shortcut on the character following the underscore. The shortcut for file will look like this $label->set_text_with_mnemonic ("_File"). Gtk2::Label class objects can not get focus. To tell the Gtk2::Label class object, with a mnemonic, which object must get the focus, use the $label->set_mnemonic_widget ($widget) , supplying it the widget to be focused on as argument. This will typically be used is where a Gtk2::Entry widget accompanies a Gtk2::Label widget. You can combine Pango markup with an mnemonic by using the following:

 	$label->set_markup_with_mnemonic ($str) 
	

6.1. The Code

The program can be found here: 'Label Demo'

Figure 6-1. Gtk2::Label

 1 #! /usr/bin/perl -w
 2 
 3 use strict;
 4 use Glib qw/TRUE FALSE/;
 5 use Gtk2 '-init'; 
 6 
 7 my $deg = 0;
 8 my $label;
 9 
10 Glib::Timeout->add (200,sub{ &rotate_label() });
11 
12 #standard window creation, placement, and signal connecting
13 my $window = Gtk2::Window->new('toplevel');
14 $window->signal_connect('delete_event' => sub { Gtk2->main_quit; });
15 $window->set_border_width(5);
16 $window->set_position('center_always');
17 
18 #this vbox will geturn the bulk of the gui
19 my $vbox = &ret_vbox();
20 
21 #add and show the vbox
22 $window->add($vbox);
23 $window->show();
24 
25 #our main event-loop
26 Gtk2->main();
27 
28 
29 sub ret_vbox {
30 
31 #create a vbox to pack the following widgets
32 my $vbox = Gtk2::VBox->new(FALSE,5);
33 
34 	#this will be the label who gets rotated throughout the program
35 	$label = Gtk2::Label->new();
36 	#start with it at 90deg to have max space alllocated to it
37 	$label->set_angle(90);
38 	$label->set_markup('<span foreground="DarkRed" size="x-large"><b>Rotating Label</b></span>');
39 		
40 $vbox->pack_start($label,FALSE,FALSE,4);	
41 
42 	#create table to pack the labels that will demo how mnemonic text work
43 	my $table = Gtk2::Table->new (2, 2, FALSE);
44 	
45 		my $lbl_mnemonic_one = Gtk2::Label->new_with_mnemonic("Label _One:");
46 		$lbl_mnemonic_one->set_alignment (1, 1);
47 	$table->attach_defaults ($lbl_mnemonic_one, 0, 1, 0, 1);
48 		my $ent_one = Gtk2::Entry->new();
49 		$lbl_mnemonic_one->set_mnemonic_widget ($ent_one); 
50 	$table->attach_defaults ($ent_one, 1, 2, 0, 1);
51 		my $lbl_mnemonic_two = Gtk2::Label->new();
52 		$lbl_mnemonic_two->set_markup_with_mnemonic('<span foreground="maroon" size="x-large"><i>Label _Two</i></span>');
53 		$lbl_mnemonic_two->set_alignment (0, 0);
54 	$table->attach_defaults ($lbl_mnemonic_two, 0, 1, 1, 2);
55 		my $ent_two = Gtk2::Entry->new();
56 		$lbl_mnemonic_two->set_mnemonic_widget ($ent_two);
57 	$table->attach_defaults ($ent_two, 1, 2, 1, 2);
58 	
59 $vbox->pack_end($table,FALSE,FALSE,4);
60 $vbox->pack_end(Gtk2::HSeparator->new(),FALSE,FALSE,4);
61 	
62 	#create a label that will demo pango markup
63 	my $label_markup = Gtk2::Label->new();
64 	$label_markup->set_markup("<span foreground=\"yellow1\" size=\"40000\">Label with</span><s><big> Tango </big></s> Pango<span background = 'black' foreground= 'green' size ='30000'><i>markup</i></span>");
65 $vbox->pack_end($label_markup,FALSE,FALSE,4);	
66 $vbox->pack_end(Gtk2::HSeparator->new(),FALSE,FALSE,4);
67 
68 $vbox->show_all();
69 
70 return $vbox;
71 
72 }
73 
74 sub rotate_label {
75 $deg = $deg +5;
76 ($deg==360)&&($deg =0);
77 $label->set_angle($deg);
78 return 1;
79 }

Looking at the interesting bits and pieces

We will look at the previous bits discussed, implemented in the sample program above.