GTK+ 2.0 Tutorial
<<< Previous | Miscellaneous Widgets | Next >>>

# 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:

    GtkWidget *gtk_statusbar_new( void );

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

    guint gtk_statusbar_get_context_id( GtkStatusbar *statusbar,
                                        const gchar  *context_description );

There are three functions that can operate on statusbars:

    guint gtk_statusbar_push( GtkStatusbar *statusbar,
                              guint         context_id,
                              const gchar  *text );

    void gtk_statusbar_pop( GtkStatusbar *statusbar)
                            guint         context_id );

    void gtk_statusbar_remove( GtkStatusbar *statusbar,
                               guint         context_id,
                               guint         message_id );

The first, gtk_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 gtk_statusbar_remove to remove the message with the given Message and Context Identifiers from the statusbar's stack.

The function gtk_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.

    void     gtk_statusbar_set_has_resize_grip( GtkStatusbar *statusbar,
    					    gboolean      setting );

    gboolean gtk_statusbar_get_has_resize_grip( GtkStatusbar *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.

    #include <stdlib.h>
    #include <gtk/gtk.h>
    #include <glib.h>

    GtkWidget *status_bar;

    void push_item( GtkWidget *widget,
                    gpointer   data )
    {
      static int count = 1;
      char buff[20];

      g_snprintf (buff, 20, "Item %d", count++);
      gtk_statusbar_push (GTK_STATUSBAR (status_bar), GPOINTER_TO_INT (data), buff);

      return;
    }

    void pop_item( GtkWidget *widget,
                   gpointer   data )
    {
      gtk_statusbar_pop (GTK_STATUSBAR (status_bar), GPOINTER_TO_INT (data));
      return;
    }

    int main( int   argc,
              char *argv[] )
    {

        GtkWidget *window;
        GtkWidget *vbox;
        GtkWidget *button;

        gint context_id;

        gtk_init (&argc, &argv);

        /* create a new window */
        window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
        gtk_widget_set_size_request (GTK_WIDGET (window), 200, 100);
        gtk_window_set_title (GTK_WINDOW (window), "GTK Statusbar Example");
        g_signal_connect (G_OBJECT (window), "delete_event",
                          G_CALLBACK (exit), NULL);

        vbox = gtk_vbox_new (FALSE, 1);
        gtk_container_add (GTK_CONTAINER (window), vbox);
        gtk_widget_show (vbox);

        status_bar = gtk_statusbar_new ();
        gtk_box_pack_start (GTK_BOX (vbox), status_bar, TRUE, TRUE, 0);
        gtk_widget_show (status_bar);

        context_id = gtk_statusbar_get_context_id(
                              GTK_STATUSBAR (status_bar), "Statusbar example");

        button = gtk_button_new_with_label ("push item");
        g_signal_connect (G_OBJECT (button), "clicked",
                          G_CALLBACK (push_item), GINT_TO_POINTER (context_id));
        gtk_box_pack_start (GTK_BOX (vbox), button, TRUE, TRUE, 2);
        gtk_widget_show (button);

        button = gtk_button_new_with_label ("pop last item");
        g_signal_connect (G_OBJECT (button), "clicked",
                          G_CALLBACK (pop_item), GINT_TO_POINTER (context_id));
        gtk_box_pack_start (GTK_BOX (vbox), button, TRUE, TRUE, 2);
        gtk_widget_show (button);

        /* always display the window as the last step so it all splashes on
         * the screen at once. */
        gtk_widget_show (window);

        gtk_main ();

        return 0;
    }

<<< Previous | Home | Next >>>
Rulers | Up | Text Entries