Jazz up your Gtk# App with Ximian's Icons

Novell Cool Solutions: Feature
By Kevin Breit

Digg This - Slashdot This

Posted: 10 Sep 2004

[Editor: We have made a minor correction -- we incorrectly cited this as being released under the GPL, when it actually is released under the LGPL. Sorry for any confusion.]

When putting together an application, many developers consider the visual appearance to be almost as important as the internal functionality of the application itself. After all, who wants to look at 16-color icons? Now if you find 16 colors to be the ultimate in visual design, then OpenOffice.org is a great program for you and you can stop reading now. But the rest of us are often annoyed by the disparity between OpenOffice.org's visual design and that of Linux or Windows. Ximian recognized this problem and made over a thousand high-color icons doused in alpha-channel goodness. That's right, OpenOffice.org on Linux shares many of the same icons as Ximian Desktop 2. If the icon isn't in Ximian Desktop 2, Jakub Steiner created icons that fit in with the Ximian Desktop 2 theme.

If you're putting together an application, you are free to use every one of these icons. Ximian has released the icons under the LGPL. This means that you can use the icons in your program free of charge. If you want to fork the icons into your own icon collection, you need to retain the license and the original copyrights, but from there you're free to do what you want.

Using the Icons

Your first step to integrating the icons is to download them.

Here's a handy tarball. Untar them in a directory of your choice by typing:

tar -xvzf ooo-icons.tar.gz

Now you will find hundreds of icons in the directory. If you're interested in seeing what they look like, Nautilus and Konqueror both offer great graphic preview features. Create a directory named icons in your project directory. Copy the icons you want to use into the newly created directory and load up the code that you use to draw your UI.

Creating a Toolbar with the new Icons

For this article, we're assuming that the program is written in Gtk#. To create a toolbar, you'd want to use something like:

Toolbar toolbar = new Toolbar();

Now every button is going to have to be defined. This is done using the Button class. Creating a button to generate a graph would look like:

Button graph = new Button();

Now that the button is defined, create the icon and pack it into the toolbar:

Gtk.Image icon = new Image("/dir/to/project/stock_chart-edit-type.png");
toolbar.AppendWidget(graph, "Graph", "Graph");

When a button is clicked, it emits a signal. This signal is what calls a function so that action can be taken on the user's behalf. The following line calls OnToolbarClicked() when the button is pushed:

graph.Clicked += new EventHandler(OnToolbarClicked);

Finally, you will want to make sure you pack the toolbar into the window's Vbox:

vbox.PackStart(toolbar, false, false, 0);

A vbox.ShowAll(); will display the toolbar with a graphing icon in place. That's it, on the coding side of things at least.

Your last task is to compile and execute the program:

mcs -r:gtk-sharp -r:gnome-sharp -r:atk-sharp -r:pango-sharp toolbar.cs -o toolbar.exe

Kevin Breit is a Novell web developer and project manager currently located in Buffalo Grove, IL.

Novell Cool Solutions (corporate web communities) are produced by WebWise Solutions. www.webwiseone.com

© Micro Focus