122. How to have a status area using GtkStatusbar?

Problem

You want to have a status bar at the bottom of your GtkWindow as shown below:

How to have a status area using GtkStatusbar?


Solution

  • Create a GtkStatusbar.
  • Create a GtkVBox if necessary, and stuff the status bar as the last widget in the vbox (so that it's at the bottom).
  • If you need the status bar to have a different background color or respond to events, stuff the status bar inside an GtkEventBox and then pack the eventbox into the vbox.
  • To display a message, we need to first get a message id with GtkStatusbar::get_context_id(), then display the message with GtkStatusbar::push().

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   
<?php
$window = new GtkWindow();
$window->set_size_request(400, 200);
$window->connect_simple('destroy', array('Gtk','main_quit'));
$window->add($vbox = new GtkVBox());

// display title
$title = new GtkLabel("Window with status area");
$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);

$vbox->pack_start(new GtkLabel('your body contents'));

// setup status area
$status = new GtkStatusbar(); // note 1
$eventbox = new GtkEventBox();
$eventbox->add($status);
$vbox->pack_start($eventbox, 0, 0); // note 2
$eventbox->modify_bg(Gtk::STATE_NORMAL, GdkColor::parse('#ffff00')); // note 3


// display a message
$context_id = $status->get_context_id('msg1'); // note 4
$status->push($context_id, 'this is msg 1'); // note 4

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

Output

As shown above.

 

Explanation

  1. Create a statu bar, stuff it inside an eventbox, and pack the eventbox into the vbox.
  2. Don't forget to set expand and fill to false when packing so that the status bar stays at the bottom.
  3. Here we set the background color of the status bar to yellow.
  4. Display a sample message.

Related Links

Add comment


Security code
Refresh