Originally Published: Wednesday, 25 July 2001 Author: Subhasish Ghosh
Published to: develop_articles/Development Articles Page: 2/3 - [Printable]

GNOME Programming in Linux using GTK+

Ever wonder how those little garden Gnomes move around and stuff at night? It's because they are programmed, of course! In this article Linux.com asked Subhasish Ghosh to show us how to start programming our own Gnome applications using the GTK+ toolkit.

Widgets  << Page 2 of 3  >>

Containers

Next, we look into another vital component of GNOME programming: Containers. GTK+ uses containers a great deal because GTK+ is actually a "container-based" toolkit. That means we have a parent container within which we have to place our other widgets. Windows are single widget containers. Thus, the important point to keep in mind is that GTK+ utilizes invisible "packing boxes" that can hold multiple widgets to create windows layouts. These "packing boxes" are of two types: horizontal and vertical, created by using the functions gtk_hbox_new and gtk_vbox_new, respectively. We will see these functions in action soon, in the applications that we create later. For now, let's take a look into the parameters of these two functions:
  1. homogeneous : type --> gboolean : Forces all widgets in the box to occupy the same area as the largest widget in the box.
  2. spacing : type --> gint : Determines the space between adjacent widgets.
  3. expand : type --> gboolean : Allows the packing box to expand to fill the remaining space.
  4. fill : type --> gboolean : Allows that particular widget to expand to fill the remaining space.
  5. padding : type --> gint : Determines the width of a frame surrounding the widget.

Buttons

Next we come to another very vital component: Buttons. GTK+ provides four different kinds of buttons:
  1. Simple push buttons --> To perform an action on clicking.
  2. Toggle buttons --> With a particular state: Up/Down
  3. Check boxes --> With a particular state: On/Off
  4. Radio buttons --> For making only one selection from a group of options.

Creating radio buttons is very similar to check boxes, and all that we need to do extra is to specify a group for the radio button to belong to. Radio buttons are derived from check buttons, which are derived from toggle buttons, so this means that we have the same set of functions to read and modify their state and also use the same old events. Please note: For more information of specific functions, consult the GTK+ Reference Documentation available at: http://www.gtk.org

Entry Widgets

For creating single-line text widgets, which are commonly called "Entry widgets", we utilize a function called gtk_entry_new( ). Entry widgets are mainly used to enter small amounts of information. Let's take a look at a program that creates a "Login Window", and outputs the password field, when the activate signal occurs, when the button is pressed. Type in the following and execute the program as has been explained above.
/* Creating a Login GNOME-style using GTK+ Toolkit:
    Created By: Subhasish Ghosh
    Date: Sunday, July 1, 2001
*/

#include <gnome.h>

static void enter_pressed(GtkWidget *button, gpointer data)
{
    GtkWidget *text_entry = data;
    char *string = gtk_entry_get_text(GTK_ENTRY(text_entry));
    g_print(string);
}

int main(int argc, char *argv[])
{
    GtkWidget *app;
    GtkWidget *text_entry;
    GtkWidget *label;
    GtkWidget *hbox;
    gchar *text;
    
    gnome_init("example", "0.1", argc, argv);
        app = gnome_app_new("example", "entry widget");
        gtk_container_border_width(GTK_CONTAINER(app), 5);

        hbox = gtk_hbox_new(FALSE, 0);
        
    /* we now create a Label: */
    label = gtk_label_new("Password: ");
        gtk_misc_set_alignment(GTK_MISC(label), 0, 1.0);
        gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 0);
        
        text_entry = gtk_entry_new();
        gtk_entry_set_visibility(GTK_ENTRY(text_entry), FALSE);
        gtk_box_pack_start(GTK_BOX(hbox), text_entry, FALSE, FALSE, 0);

    gtk_signal_connect(GTK_OBJECT(app), "delete_event", GTK_SIGNAL_FUNC(gtk_main_quit), NULL);
    gtk_signal_connect(GTK_OBJECT(text_entry), "activate", GTK_SIGNAL_FUNC(enter_pressed), text_entry);

    gnome_app_set_contents(GNOME_APP(app), hbox);
        gtk_widget_show_all(app);
        gtk_main();
        return 0;
}


When executed this program will cause a login window to appear on the screen. Type in any text (assuming it to be a password), and press enter. Observe what happens.





Widgets  << Page 2 of 3  >>