Home   |   Guides and Tutorials   |   What's New?   |   Comments   |   About
 

GtkVBox - Vertical Container

by Brent Fox
Last Modified: Wednesday, 19-May-2004 11:54:43 EDT

Introduction
    A special set of GTK+ widgets called containers allow the programmer to control the placement of GTK+ widgets on the screen. Individual GTK+ widgets such as buttons and text area are grouped together into one or more containers. The containers are then grouped together inside a parent container such as the main window. Containers can be nested inside one another. In this example, we will look at a very simple container called the GtkVBox. The GtkVBox container is used to group widgets in a vertical stack from top to bottom and can contain any kind of GTK+ widget. In this example, the GtkVBox contains three GtkLabel widgets and three GtkButton widgets.
Screenshot



Source Code
Source code for this example is also available in the file vbox.c
/*
 *File name: vbox.c
 */

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

/*-- This function allows the program to exit properly when the window is closed --*/
gint destroyapp (GtkWidget *widget, gpointer gdata)
{
  g_print ("Quitting...\n");
  gtk_main_quit();
  return (FALSE);
}

/*-- This function responds to the mouse click on the button --*/
void button_clicked(GtkWidget *widget, gpointer gdata)
{
  g_print("Button was clicked.\n");
}

int main (int argc, char *argv[])
{
  /*-- Declare the GTK Widgets used in the program --*/
  GtkWidget *window;
  GtkWidget *label1;
  GtkWidget *label2;
  GtkWidget *label3;
  GtkWidget *button1;
  GtkWidget *button2;
  GtkWidget *button3;
  GtkWidget *vbox;

  /*--  Initialize GTK --*/
  gtk_init (&argc, &argv);

  /*-- Create the new window --*/
  window = gtk_window_new(GTK_WINDOW_TOPLEVEL);

  /*-- Create the labels --*/
  label1 = gtk_label_new("Label1");
  label2 = gtk_label_new("Label2");
  label3 = gtk_label_new("Label3");

  /*-- Create some buttons to fill the vbox with --*/
  button1 = gtk_button_new_with_label("Button 1");
  button2 = gtk_button_new_with_label("Button 2");
  button3 = gtk_button_new_with_label("Button 3");

  /*-- Create the vbox --*/
  vbox = gtk_vbox_new(FALSE,0);

  /*-- Connect the window to the destroyapp function  --*/
  gtk_signal_connect(GTK_OBJECT(window), "delete_event", GTK_SIGNAL_FUNC(destroyapp), NULL);

  /*-- Connect all the buttons to the button_was_clicked function --*/
  gtk_signal_connect(GTK_OBJECT(button1), "clicked", GTK_SIGNAL_FUNC(button_clicked), NULL);
  gtk_signal_connect(GTK_OBJECT(button2), "clicked", GTK_SIGNAL_FUNC(button_clicked), NULL);
  gtk_signal_connect(GTK_OBJECT(button3), "clicked", GTK_SIGNAL_FUNC(button_clicked), NULL);

  /*-- Add all the buttons to the vbox --*/
  gtk_box_pack_start(GTK_BOX(vbox), label1, FALSE, FALSE, 0); 
  gtk_box_pack_start(GTK_BOX(vbox), button1, FALSE, FALSE, 0);
  gtk_box_pack_start(GTK_BOX(vbox), label2, FALSE, FALSE, 0); 
  gtk_box_pack_start(GTK_BOX(vbox), button2, FALSE, FALSE, 0);
  gtk_box_pack_start(GTK_BOX(vbox), label3, FALSE, FALSE, 0);   
  gtk_box_pack_start(GTK_BOX(vbox), button3, FALSE, FALSE, 0);
   
  /*-- Add the button to the window --*/
  gtk_container_add(GTK_CONTAINER (window), vbox);

  /*-- Add a border to the window to give the buttons a little room --*/
  gtk_container_border_width (GTK_CONTAINER (window), 15);

  /*-- Display the widgets --*/
  gtk_widget_show(vbox);
  gtk_widget_show(label1);
  gtk_widget_show(label2);
  gtk_widget_show(label3);
  gtk_widget_show(button1);
  gtk_widget_show(button2);
  gtk_widget_show(button3);
  gtk_widget_show(window);

  /*-- Start the GTK event loop --*/
  gtk_main();

  /*-- Return 0 if exit is successful --*/
  return 0;
}

Compile the Source Code
gcc -Wall -g vbox.c -o vbox `gtk-config --cflags` `gtk-config --libs`

Execute the Program
./vbox

What's Related


All Rights Reserved Linux Headquarters © 2000-2007
Linux is a registered trademark of Linus Torvalds
All logos are registered trademarks of their respective owners
Last modified: Wednesday, May 19, 2004