Chapter 11. Miscellaneous Widgets

Table of Contents

1. Labels
2. Arrows
3. The Tooltips Object
4. Progress Bars
5. Dialogs
6. Rulers
7. Statusbars
8. Text Entries
9. Spin Buttons
10. Combo Box
11. Calendar
12. Color Selection
13. File Selections

1. Labels

Labels are used a lot in GTK, and are relatively simple. Labels emit no signals as they do not have an associated X window. If you need to catch signals, or do clipping, place it inside a EventBox widget or a Button widget.

To create a new label, use:

$label = Gtk2::Label->new($str=undef);

$label = Gtk2::Label->new_with_mnemonic($str);

The sole argument is the string you wish the label to display.

To change the label's text after creation, use the function:

Gtk2::Label->set_text($label, $string);

The first argument is the label widget you created previously, and the second is the new string.

The space needed for the new string will be automatically adjusted if needed. You can produce multi-line labels by putting line breaks in the label string.

To retrieve the current string, use:

$text = Gtk2::Label->get_text($label);                    

The label text can be justified using:

Gtk2::Label->set_justify($label, $jtype);

Values for jtype are:

  center (the default)

The label widget is also capable of line wrapping the text automatically. This can be activated using:

Gtk2::Label->set_line_wrap($label, $wrap);

The wrap argument takes a TRUE or FALSE value.

If you want your label underlined, then you can set a pattern on the label:

Gtk2::Label->set_pattern($label, $pattern);

The pattern argument indicates how the underlining should look. It consists of a string of underscore and space characters. An underscore indicates that the corresponding character in the label should be underlined. For example, the string "__ __" would underline the first two characters and eight and ninth characters.


If you simply want to have an underlined accelerator ("mnemonic") in your label, you should use Gtk2::Label::new_with_mnemonic() or Gtk2::Label::set_text_with_mnemonic(), not Gtk2::Label::set_pattern().

Below is a short example to illustrate these functions. This example makes use of the Frame widget to better demonstrate the label styles. You can ignore this for now as the Frame widget is explained later on.

In GTK+ 2.0, label texts can contain markup for font and other text attribute changes, and labels may be selectable (for copy-and-paste). These advanced features won't be explained here.

use Glib qw/TRUE FALSE/;
use Gtk2 '-init';

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

$vbox = Gtk2::VBox->new(FALSE, 5);
$hbox = Gtk2::HBox->new(FALSE, 5);
$hbox->pack_start($vbox, FALSE, FALSE, 0);

$frame = Gtk2::Frame->new("Normal Label");
$label = Gtk2::Label->new("This is a Normal label");
$vbox->pack_start($frame, FALSE, FALSE, 0);

$frame = Gtk2::Frame->new("Multi-line Label");
$label = Gtk2::Label->new("This is a Multi-line label.\nSecond line\n" .
                          "Third line");
$vbox->pack_start($frame, FALSE, FALSE, 0);

$frame = Gtk2::Frame->new("Right Justified Label");
$label = Gtk2::Label->new("This is a Right-Justified\nMulti-line label.\n" .
                          "Fourth line, (j/k)");
$vbox->pack_start($frame, FALSE, FALSE, 0);

$vbox = Gtk2::VBox->new(FALSE, 5);
$hbox->pack_start($vbox, FALSE, FALSE, 0);
$frame = Gtk2::Frame->new("Line wrapped label");
$label = Gtk2::Label->new("This is an example of a line-wrapped label.  It " .
                          "should not be taking up the entire              " .
			  "width allocated to it, but automatically "        .
			  "wraps the words to fit.  "                        .
			  "The time has come, for all good men, to come to " .
			  "the aid of their party.  "                        .
			  "The sixth sheik's six sheep's sick.\n"            .
			  "     It supports multiple paragraphs correctly, " .
			  "and  correctly   adds "                           .
			  "many          extra  spaces.");
$vbox->pack_start($frame, FALSE, FALSE, 0);

$frame = Gtk2::Frame->new("Filled, wrapped label");
$label = Gtk2::Label->new("This is an example of a line-wrapped, filled label.  " .
                          "It should be taking "			          .
			  "up the entire              width allocated to it.  "   .
			  "Here is a sentence to preve " 		          .
			  "my point.  Here is another sentence. "		  .
			  "Here comes the sun, do de do de do.\n"		  .
			  "    This is a new paragraph.\n"			  .
			  "    This is another news, longer, better "		  .
			  "paragraph.  It is coming to an end, "		  .
$vbox->pack_start($frame, FALSE, FALSE, 0);

$frame = Gtk2::Frame->new("Underlined label");
$label = Gtk2::Label->new("This label is underlined!\n" .
                          "This one is underlined in quite a funky fashion");
$label->set_pattern("_________________________ _ _________ _ ________ _______ ___");
$vbox->pack_start($frame, FALSE, FALSE, 0);