Gtk# Frequently Asked Questions


  1. General
    1. What is Gtk#?
    2. Why isn't it called Gnome# or Gnome.Net?
  2. Compiling Gtk#
    1. What are the Dependencies to build Gtk# on X11 platforms?
    2. What about building on Win32 platforms?
  3. Developing with Gtk#
    1. Why can't mono find gtk-sharp.dll?
    2. Why can't my compiler find Gtk# classes?
    3. Why don't I get ButtonPressEvents from my Button/TreeView?
    4. Why do I get DllNotFoundExceptions for glibsharpglue?
    5. Why does my multithreaded app hang?

1.1  What is Gtk#?

Gtk# brings the power and ease of .Net development paradigms to the free software GUI application development community.  Gtk# provides a C source parser and C# code generator which can be used to produce .Net assemblies that bind to GObject based libraries.  Among the libraries currently wrapped are gtk, gdk, atk, pango, gda, gnomedb, gconf, libgnomecanvas, libgnomeui, vte, librsvg, and libglade.

1.2  Why isn't it called Gnome# or Gnome.Net?

When the project was started, the idea was to wrap the Gtk+ libraries with a set of C# classes.  It quickly became apparent that the source parser and C# code generator could be used to wrap any GObject based API.  Contributors started wrapping their favorite GNOME libraries.  Mailing lists and a sourceforge project were already open under the gtk-sharp nomenclature and no compelling technical reasons for renaming presented themselves.  The decision was made to continue under the Gtk# name, even though the binding encompasses more than just Gtk, and more than just C#.

2.1  What are the Dependencies to build Gtk# on X11 platforms?

You'll need a .Net framework installed. Gtk# has been successfully used with both the mono and Portable.Net frameworks. The bare minimum requirement is a complete installation of the Gtk+-2.0 libraries.  Keep in mind that if your Gtk+ installation is provided by binary packages, you will also need to install the *-devel packages.  If you have additional GNOME-2.0 libraries installed, the configure script will detect their presence and the associated wrappers will be built.  A summary of the libraries which will be built is printed at the end of the configure step.

2.2  What about building on Win32 platforms?

To build Gtk# on win32, you will need a working cygwin installation.  Keep in mind that the default cygwin installation may not have all the tools you need.  Cygwin provides the gnu make tool which builds the Gtk# library.  You will also need the mingw-gcc compiler installed in order to build the Gtk# glue library.  There are binary packages of the Gtk+-2.0 libs and dependencies available from the Gtk+ for Win32 project page.  You will also need these libraries installed, along with their devel packages in order to build the glue library.  Once you have this environment set up, expand the gtk-sharp source into a directory and in a cygwin shell window, cd to the gtk-sharp directory and execute the command "make -f makefile.win32".

3.1  Why can't mono find gtk-sharp.dll?

You most likely have installed Gtk# into a prefix other than where mono is installed.  In order for the runtime to locate the Gtk# assemblies, you will need to add their location to the MONO_GAC_PREFIX environment variable. For the default installation prefix, this will mean executing the following command:
export MONO_GAC_PREFIX=/usr/local:$MONO_GAC_PREFIX


3.2  Why can't my compiler find the Gtk# classes?

You need to provide an assembly reference in your compilation command. The easiest way to add references is to use the -pkg: switch to mcs. For the GLib, Pango, Atk, Gdk, and Gtk namespaces, use -pkg:gtk-sharp. There are also pkg options for each of the optional assemblies (e.g. gnome-sharp).

3.3  Why don't I get ButtonPressEvents from my Button/Treeview?

As of release 0.15, Gtk# started using the CONNECT_AFTER flag when connecting event handlers to signals. This means that the event handlers are not run until after the default signal handlers, which means that the widget will be updated when the event handlers run. A side effect of this change is that in the case where default handlers return true to stop signal propogation, Gtk# events will not be emitted. This is the case for example in Gtk.Button, where the button-press-event default signal handler is overridden to emit Pressed events. TreeView has a similar situation.

While potentially confusing, this is not really a bug. When you use a Gtk.Button, you are getting a widget that emits Pressed events in response to Button1 presses. If you also want your Button to change colors, or popup a context menu on Button3 presses, that's not a Gtk.Button. The correct way to implement such a widget is to subclass Gtk.Button and override the OnButtonPressEvent virtual method to implement the new behaviors you desire.

Many Gtk+ developers have gotten used to being able to do these kind of things via signal handlers since g_signal_connect is typically used without the CONNECT_AFTER flag in C code. If you are convinced that you want to use g_signal_connect to pierce the encapsulation of a Widget and alter its default behavior, you can tag your callback method with the [GLib.ConnectBefore] attribute to force it to run before the default handler. This attribute was added in version 0.18 due to public outcry, but the default behavior is still to run event handlers after the default handler.

3.4  Why do I get DllNotFoundExceptions for glibsharpglue?

You have most likely installed Gtk# into a prefix that is not listed in /etc/ld.so.conf or your LD_LIBRARY_PATH. You either need to add the prefix/lib path (e.g. /usr/local/lib) to the /etc/ld.so.conf file and execute ldconfig, or add the path to your LD_LIBRARY_PATH environment variable and export it to your shell (i.e. export LD_LIBRARY_PATH=/usr/local/lib).

3.5  Why does my multithreaded app hang?

Gtk# is not threadsafe. Neither is System.Windows.Forms. Gtk+ is "thread aware" and provides a mechanism for locking and unlocking the UI updates, but you are still responsible for ensuring that all updates to the GUI come from the main thread where you called Run (). Gtk# provides a helper class called ThreadNotify to assist in the context switch from different threads to the GUI thread. In addition, using an Idle handler or a GLib.TimeoutHandler with a very short interval will ensure the context switch.