7. Statusbars

Statusbars are simple widgets used to display a text message. They keep a stack of the messages pushed onto them, so that popping the current message will re-display the previous text message.

In order to allow different parts of an application to use the same statusbar to display messages, the statusbar widget issues Context Identifiers which are used to identify different "users". The message on top of the stack is the one displayed, no matter what context it is in. Messages are stacked in last-in-first-out order, not context identifier order.

A statusbar is created with a call to:

$statusbar = Gtk2::Statusbar->new;

A new Context Identifier is requested using a call to the following function with a short textual description of the context:

integer = Gtk2::Statusbar->get_context_id($statusbar, $context_description);

There are three functions that can operate on statusbars:

integer = Gtk2::Statusbar->push($statusbar, $context_id, $text);

Gtk2::Statusbar->pop($statusbar, $context_id);

Gtk2::Statusbar->remove($statusbar, $context_id, $message_id ); 

The first, Gtk2::Statusbar::push(), is used to add a new message to the statusbar. It returns a Message Identifier, which can be passed later to the function Gtk2::Statusbar::remove() to remove the message with the given Message and Context Identifiers from the statusbar's stack.

The function Gtk2::Statusbar::pop() removes the message highest in the stack with the given Context Identifier.

In addition to messages, statusbars may also display a resize grip, which can be dragged with the mouse to resize the toplevel window containing the statusbar, similar to dragging the window frame. The following functions control the display of the resize grip.

Gtk2::Statusbar->set_has_resize_grip($statusbar, $setting);

boolean = Gtk2::Statusbar->get_has_resize_grip($statusbar);

The following example creates a statusbar and two buttons, one for pushing items onto the statusbar, and one for popping the last item back off.



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

# create a new window
my $window = Gtk2::Window->new('toplevel');
$window->set_size_request(200, 100);
$window->set_title("gtk2-perl Statusbar Example");
$window->signal_connect(delete_event => sub { Gtk2->main_quit; FALSE; });

my $vbox = Gtk2::VBox->new(FALSE, 1);
$window->add($vbox);
$vbox->show;

my $status_bar = Gtk2::Statusbar->new;
$vbox->pack_start($status_bar, TRUE, TRUE, 0);
$status_bar->show;
$status_bar->{count} = 1;

my $context_id = $status_bar->get_context_id("Statusbar example");

$button = Gtk2::Button->new("push item");
$button->signal_connect(clicked => sub {
		$status_bar->push($context_id,
		                  sprintf("Item %d", $status_bar->{count}++));
	});
$vbox->pack_start($button, TRUE, TRUE, 2);
$button->show;

$button = Gtk2::Button->new("pop item");
$button->signal_connect(clicked => sub {
		$status_bar->pop($context_id);
	});
$vbox->pack_start($button, TRUE, TRUE, 2);
$button->show;

# always display the window as the last step so it all splashes on
# the screen at once.
$window->show;

Gtk2->main;

0;