Originally Published: Wednesday, 25 July 2001 Author: Subhasish Ghosh
Published to: develop_articles/Development Articles Page: 3/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.

More Widgets  << Page 3 of 3  

List boxes and Combo boxes

List boxes and Combo boxes play the same role as they play on the Microsoft platform. List box widgets hold a list of strings that allow users to select one or more entries. Combo boxes are entry widgets with an added pull down menu that allows users to select options.

Menus & Toolbars

The various widgets that we have come across until now are simple widgets that don't provide extra-ordinary functionality. We now look at some specific GNOME programming libraries that offer complicated widgets with rich functionality.

Just hang on for a second, someone may ask: "Hey, we were doing pretty well with ordinary code and all the stuff that you discussed earlier. What's the use of this so-called "specific GNOME programming libraries"? Are they indeed useful? Or are you just including them here for making your article a bit longer?"

Well, here's the reason for considering specific GNOME programming libraries. With plain GTK+ code, though nearly everything can be done that we would usually do using non-specific GNOME programming libraries, using simple and plain GTK+ code often leads to much code-repetition, in-efficient code-blocks and such other things, finally making the whole program a bloated one. Now, to prevent this from happening, we use specific GNOME programming libraries that provide a great deal of extra functionality with much lower programming overheads.

So, let's talk about "Menus" and "Toolbars" in this section. GNOME let's us create menus and toolbars for our GnomeApp widgets that can be docked and undocked from the window. The arrays need to be filled with necessary information, and then call gnome_app_create_menus or gnome_app_create_toolbar.

The menus and toolbar items each have properties. The arrays (we earlier talked about) are a list of structures that define these properties. A few such properties include type, string, callback pointer etc. For the majority of the time menu entries are pretty simple, and we can just use one of a set of macros provided by GNOME to create the structure for us. So, let's check out a few most used top-level macros.

Please note: These top-level macros are the ones that create top-level menus when passed an array that can contain any or all of the following GnomeUIInfo structures.

Menu Macro File GNOMEUIINFO_MENU_FILE_TREE(tree) Edit GNOMEUIINFO_MENU_EDIT_TREE(tree) View GNOMEUIINFO_MENU_VIEW_TREE(tree) Settings GNOMEUIINFO_MENU_SETTINGS_TREE(tree) Windows GNOMEUIINFO_MENU_WINDOWS_TREE(tree) Help GNOMEUIINFO_MENU_HELP_TREE(tree) Game GNOMEUIINFO_MENU_GAME_TREE(tree)

Within the top-level menu there exists over thirty macros for creating common menu items. The macros associate small images (pixmaps) and accelerator keys with each menu item. A callback function is required to be called when the item is selected and a data pointer is to be passed to that function.

Let's look at some of these common menu items and their respective macros.

File  --> New --> GNOMEUIINFO_MENU_NEW_ITEM (label, hint, cb, data)
    Open --> GNOMEUIINFO_MENU_OPEN_ITEM (cb, data)
    Save --> GNOMEUIINFO_MENU_SAVE_ITEM (cb, data)
    Print --> GNOMEUIINFO_MENU_PRINT_ITEM (cb, data)
    Exit --> GNOMEUIINFO_MENU_EXIT_ITEM (cb, data)

Edit  --> Cut --> GNOMEUIINFO_MENU_CUT_ITEM (cb, data)
    Copy --> GNOMEUIINFO_MENU_COPY_ITEM (cb, data)
    Paste --> GNOMEUIINFO_MENU_PASTE_ITEM (cb, data)

Settings --> Preferences --> GNOMEUIINFO_MENU_PREFERENCES_ITEM (cb, data)

Help --> About --> GNOMEUIINFO_MENU_ABOUT_ITEM (cb, data)


Just as with menu bars, toolbars require an array using the GNOMEUIINFO_ITEM_STOCK (label, tooltip, callback, stock_id) macro. Here, "stock_id" is the id of a predefined icon that we want to use for that item.

Let's look at this example and see how the arrays and macros work in reality.


#include <gnome.h>

static void callback (GtkWidget *button, gpointer data)
{
    g_print("Item Selected");
}

GnomeUIInfo file_menu[ ] = {
    GNOMEUIINFO_ITEM_NONE ("A menu item", "This is the Status bar info", callback),
    GNOMEUIINFO_MENU_EXIT_ITEM (gtk_main_quit, NULL),
    GNOMEUIINFO_END
};

GnomeUIInfo menubar[ ] = {
    GNOMEUIINFO_MENU_FILE_TREE (file_menu),
    GNOMEUIINFO_END
};

GnomeUIInfo toolbar[ ] = {
    GNOMEUIINFO_ITEM_STOCK ("Print", "This is another tooltip", callback, GNOME_STOCK_PIXMAP_PRINT),
    GNOMEUIINFO_ ITEM_STOCK ("Exit", "Exit the application", gtk_main_quit, GNOME_STOCK_PIXMAP_EXIT),
    GNOMEUIINFO_END
};

int main (int argc, char *argv[ ])
{
    GtkWidget *app;
    
gnome_init ("example", "0.1", argc, argv);
app = gnome_app_new ("example", "A Sample Toolbar and Menu");

gnome_app_create_menus (GNOME_APP (app), menubar);
gnome_app_create_toolbar (GNOME_APP (app), toolbar);

gtk_widget_showall (app);
gtk_main( )

return 0;
}


When executed this program will create a small window with an embedded menu and toolbar. It can be clicked, docked, undocked and dragged around the screen.

Dialog Boxes

Let's now look at the widget that displays textual information to the user in the GNOME environment. Yes, we are referring to the Dialog box. When we need to create dialog boxes, we call the gnome_message_box_new function and pass it the message text, also mention the type of dialog box we need, and the buttons we want on it. All of this is included in a NULL terminated list. Then we bind the "clicked" signal of the dialog widget that we have just created to a handling function that is passed the button the user pressed as an integer. Finally, we call the gtk_widget_show function for displaying a non-modal box.

Let's look at this code extract from a program that creates a simple question dialog box, adds three buttons and responds to the user's code.


static void messagebox_clicked(GnomeDialog *dlg, gint button, gpointer data)
{
    switch (button)
    {
        case 1:         /* user pressed apply */
            return;
        case 0:     /* user pressed ok */
        case 2:     /* user pressed close */
            gnome_dialog_close(dlg);
    }
}

GtkWidget *dlg;

dlg = gnome_message_box_new("Hi, pal, How are you doing??? I am fine!",
                    GNOME_MESSAGE_BOX_QUESTION,
                    GNOME_STOCK_BUTTON_OK,
                    GNOME_STOCK_BUTTON_APPLY,
                    GNOME_STOCK_BUTTON_CLOSE,
                    NULL);

gtk_signal_connect (GTK_OBJECT(dlg), "clicked", GTK_SIGNAL_FUNC(messagebox_clicked), NULL); 
gtk_widget_show (dlg);


Conclusion & Links for Further study

This sums up our journey of the exciting world of GNOME Programming in Linux using GTK+ Toolkit. Please note: GNOME Programming using GTK+ Toolkits is not at all difficult, it's a matter of just a little understanding that makes it real easy to grasp. Though this article just barely manages to scratch the surface of this topic, GNOME Programming by itself is a huge topic, but if worked diligently, can definitely be mastered.

For more information and detailed coverage of this topic, check out the following links that I am sure would provide rich information on Gnome programming using GTK+ toolkit and more:

About the Author: My name is Subhasish Ghosh. I am 20, currently a computer-systems engineering student in India. I am a Microsoft Certified Professional (MCP), MCP certified on NT 4.0, recently completed Red Hat Linux Certified Engineer (RHCE) Training. I have been working with Linux for a long time now, have had programmed using C, C++, VC++, COM, MFC, DCOM, ATL 3.0, Perl, Python and Linux programming using GTK+. Currently busy learning the Linux Kernel Architecture in detail, writing articles for Linux.com and most importantly practicing non-geek talk with my girlfriend, Hanna. E-mail: auspicious_blessingsindia@hotmail.com





More Widgets  << Page 3 of 3