- Preparation in GTK+ 2.x
- Changes that need to be done at the time of the switch
- Replace size_request by get_preferred_width/height
- Replace GdkRegion by cairo_region_t
- Replace GdkPixmap by cairo surfaces
- Replace GdkColormap by GdkVisual
- GdkDrawable is gone
- Event filtering
- Backend-specific code
- GtkPlug and GtkSocket
- The GtkWidget::draw signal
- GtkProgressBar orientation
- Check your expand and fill flags
- Scrolling changes
- GtkObject is gone
- GtkEntryCompletion signal parameters
- Resize grips
- Prevent mixed linkage
- Install GTK+ modules in the right place
GTK+ 3 is a major new version of GTK+ that breaks both API and ABI compared to GTK+ 2.x, which has remained API- and ABI-stable for a long time. Thankfully, most of the changes are not hard to adapt to and there are a number of steps that you can take to prepare your GTK+ 2.x application for the switch to GTK+ 3. After that, there's a small number of adjustments that you may have to do when you actually switch your application to build against GTK+ 3.
The steps outlined in the following sections assume that your application is working with GTK+ 2.24, which is the final stable release of GTK+ 2.x. It includes all the necessary APIs and tools to help you port your application to GTK+ 3. If you are still using an older version of GTK+ 2.x, you should first get your application to build and work with 2.24.
With GTK+ 2.x it was common to include just the header files for a few widgets that your application was using, which could lead to problems with missing definitions, etc. GTK+ 3 tightens the rules about which header files you are allowed to include directly. The allowed header files are are
|
for GTK |
|
for the X-specfic widgets GtkSocket and GtkPlug |
|
for low-level, UNIX-specific printing functions |
|
for GDK |
|
for GDK functions that are X11-specific |
|
for GDK functions that are Windows-specific |
(these relative paths are assuming that you are using the include
paths that are specified in the gtk+-2.0.pc file, as returned by
pkg-config --cflags gtk+-2.0.pc
.)
To check that your application only includes the allowed headers, you can use defines to disable inclusion of individual headers, as follows:
make CFLAGS+="-DGTK_DISABLE_SINGLE_INCLUDES"
Over the years, a number of functions, and in some cases, entire widgets have been deprecated. These deprecations are clearly spelled out in the API reference, with hints about the recommended replacements. The API reference for GTK+ 2 also includes an index of all deprecated symbols.
To verify that your program does not use any deprecated symbols, you can use defines to remove deprecated symbols from the header files, as follows:
make CFLAGS+="-DGDK_DISABLE_DEPRECATED -DGTK_DISABLE_DEPRECATED"
Note that some parts of our API, such as enumeration values, are
not well covered by the deprecation warnings. In most cases, using
them will require you to also use deprecated functions, which will
trigger warnings. But some things, like the GTK_DIALOG_NO_SEPARATOR
flag that has disappeared in GTK+ 3, may not.
GTK+ 3 removes many implementation details and struct members from its public headers.
To ensure that your application does not have problems with this, you
define the preprocessor symbol GSEAL_ENABLE
while
building your application against GTK+ 2.x. This will make the compiler
catch all uses of direct access to struct fields so that you can go
through them one by one and replace them with a call to an accessor
function instead.
make CFLAGS+="-DGSEAL_ENABLE"
While it may be painful to convert, this helps us keep API and ABI compatibility when we change internal interfaces. As a quick example, when adding GSEAL_ENABLE, if you see an error like:
error: 'GtkToggleButton' has no member named 'active'
this means that you are accessing the public structure of GtkToggleButton directly, perhaps with some code like:
1 2 3 4 5 6 7 8 |
static void on_toggled (GtkToggleButton *button) { if (button->active) frob_active (); else frob_inactive (); } |
In most cases, this can easily be replaced with the correct accessor method. The main rule is that if you have code like the above which accesses the "active" field of a "GtkToggleButton", then the accessor method becomes "gtk_toggle_button_get_active":
1 2 3 4 5 6 7 8 |
static void on_toggled (GtkToggleButton *button) { if (gtk_toggle_button_get_active (button)) frob_active (); else frob_inactive (); } |
In the case of setting field members directly, there's usually a corresponding setter method.
Key constants have gained a _KEY_
infix.
For example, GDK_a
is now
GDK_KEY_a
. In GTK+ 2, the old names continue
to be available. In GTK+ 3 however, the old names will require
an explicit include of the gdkkeysyms-compat.h
header.
The gdk_spawn
family of functions has been
deprecated in GDK 2.24 and removed from GDK 3. Various replacements
exist; the best replacement depends on the circumstances:
- If you are opening a document or URI by launching a command
like
firefox http://my-favourite-website.com
orgnome-open ghelp:epiphany
, it is best to just usegtk_show_uri_on_window()
; as an added benefit, your application will henceforth respect the users preference for what application to use and correctly open links in sandboxed applications. - If you are launching a regular, installed application that
has a desktop file, it is best to use GIOs GAppInfo with a suitable
launch context.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
GAppInfo *info; GAppLaunchContext *context; GError *error = NULL; info = (GAppInfo*) g_desktop_app_info_new ("epiphany.desktop"); context = (GAppLaunchContext*) gdk_display_get_app_launch_context (display); g_app_info_launch (info, NULL, context, &error); if (error) { g_warning ("Failed to launch epiphany: %s", error->message); g_error_free (error); } g_object_unref (info); g_object_unref (context);
gio/gdesktopappinfo.h
and use thegio-unix-2.0
pkg-config file when usingg_desktop_app_info_new()
. - If you are launching a custom commandline, you can
still use
g_app_info_launch()
with a GAppInfo that is constructed withg_app_info_create_from_commandline()
, or you can use the more lowlevelg_spawn
family of functions (e.g.g_spawn_command_line_async()
), and passDISPLAY
in the environment.gdk_screen_make_display_name()
can be used to find the right value for theDISPLAY
environment variable.
In GTK+ 3, the GDK drawing API (which closely mimics the X drawing API, which is itself modeled after PostScript) has been removed. All drawing in GTK+ 3 is done via cairo.
The GdkGC and GdkImage objects, as well as all the functions using
them, are gone. This includes the gdk_draw
family
of functions like gdk_draw_rectangle()
and gdk_draw_drawable()
. As
GdkGC is roughly equivalent to cairo_t and GdkImage was used for
drawing images to GdkWindows, which cairo supports automatically,
a transition is usually straightforward.
The following examples show a few common drawing idioms used by applications that have been ported to use cairo and how the code was replaced.
Example 38. Drawing a GdkPixbuf onto a GdkWindow
Drawing a pixbuf onto a drawable used to be done like this:
1 2 3 4 5 6 7 8 9 |
gdk_draw_pixbuf (window, gtk_widget_get_style (widget)->black_gc, pixbuf, 0, 0 x, y, gdk_pixbuf_get_width (pixbuf), gdk_pixbuf_get_height (pixbuf), GDK_RGB_DITHER_NORMAL, 0, 0); |
Doing the same thing with cairo:
1 2 3 4 |
cairo_t *cr = gdk_cairo_create (window); gdk_cairo_set_source_pixbuf (cr, pixbuf, x, y); cairo_paint (cr); cairo_destroy (cr); |
Note that very similar code can be used when porting code
using GdkPixmap to cairo_surface_t by calling
cairo_set_source_surface()
instead of
gdk_cairo_set_source_pixbuf()
.
Example 39. Drawing a tiled GdkPixmap to a GdkWindow
Tiled pixmaps are often used for drawing backgrounds. Old code looked something like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
GdkGCValues gc_values; GdkGC *gc; /* setup */ gc = gtk_widget_get_style (widget)->black_gc; gdk_gc_set_tile (gc, pixmap); gdk_gc_set_fill (gc, GDK_TILED); gdk_gc_set_ts_origin (gc, x_origin, y_origin); /* use */ gdk_draw_rectangle (window, gc, TRUE, 0, 0, width, height); /* restore */ gdk_gc_set_tile (gc, NULL); gdk_gc_set_fill (gc, GDK_SOLID); gdk_gc_set_ts_origin (gc, 0, 0); |
The equivalent cairo code to draw a tiled surface looks like this:
1 2 3 4 5 6 7 8 9 10 |
cairo_t *cr; cairo_surface_t *surface; surface = ... cr = gdk_cairo_create (window); cairo_set_source_surface (cr, surface, x_origin, y_origin); cairo_pattern_set_extend (cairo_get_source (cr), CAIRO_EXTEND_REPEAT); cairo_rectangle (cr, 0, 0, width, height); cairo_fill (cr); cairo_destroy (cr); |
The surface here can be either an image surface or a X surface,
and can either be created on the spot or kept around for caching purposes.
Another alternative is to use pixbufs instead of surfaces with
gdk_cairo_set_source_pixbuf()
instead of cairo_set_source_surface()
.
Example 40. Drawing a PangoLayout to a clipped area
Drawing layouts clipped is often used to avoid overdraw or to allow drawing selections. Code would have looked like this:
1 2 3 4 5 6 7 8 9 |
GdkGC *gc; /* setup */ gc = gtk_widget_get_style (widget)->text_gc[state]; gdk_gc_set_clip_rectangle (gc, &area); /* use */ gdk_draw_layout (drawable, gc, x, y, layout); /* restore */ gdk_gc_set_clip_rectangle (gc, NULL); |
With cairo, the same effect can be achieved using:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
GtkStyleContext *context; GtkStateFlags flags; GdkRGBA rgba; cairo_t *cr; cr = gdk_cairo_create (drawable); /* clip */ gdk_cairo_rectangle (cr, &area); cairo_clip (cr); /* set the correct source color */ context = gtk_widget_get_style_context (widget)); state = gtk_widget_get_state_flags (widget); gtk_style_context_get_color (context, state, &rgba); gdk_cairo_set_source_rgba (cr, &rgba); /* draw the text */ cairo_move_to (cr, x, y); pango_cairo_show_layout (cr, layout); cairo_destroy (cr); |
Clipping using cairo_clip()
is of course not restricted to text
rendering and can be used everywhere where GC clips were used.
And using gdk_cairo_set_source_color()
with style colors should
be used in all the places where a style’s GC was used to achieve
a particular color.
No more stippling.
Stippling is the usage of a bi-level mask, called a GdkBitmap.
It was often used to achieve a checkerboard effect. You can use
cairo_mask()
to achieve this effect. To get a checkerbox mask,
you can use code like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
static cairo_pattern_t * gtk_color_button_get_checkered (void) { /* need to respect pixman's stride being a multiple of 4 */ static unsigned char data[8] = { 0xFF, 0x00, 0x00, 0x00, 0x00, 0xFF, 0x00, 0x00 }; cairo_surface_t *surface; cairo_pattern_t *pattern; surface = cairo_image_surface_create_for_data (data, CAIRO_FORMAT_A8, 2, 2, 4); pattern = cairo_pattern_create_for_surface (surface); cairo_surface_destroy (surface); cairo_pattern_set_extend (pattern, CAIRO_EXTEND_REPEAT); cairo_pattern_set_filter (pattern, CAIRO_FILTER_NEAREST); return pattern; } |
Note that stippling looks very outdated in UIs, and is rarely used in modern applications. All properties that made use of stippling have been removed from GTK+ 3. Most prominently, stippling is absent from text rendering, in particular GtkTextTag.
Using the target also as source or mask.
The gdk_draw_drawable()
function allowed using the same drawable
as source and target. This was often used to achieve a scrolling
effect. Cairo does not allow this yet. You can however use
cairo_push_group()
to get a different intermediate target that
you can copy to. So you can replace this code:
1 2 3 4 5 6 |
gdk_draw_drawable (pixmap, gc, pixmap, area.x + dx, area.y + dy, area.x, area.y, area.width, area.height); |
By using this code:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
cairo_t *cr = cairo_create (surface); /* clipping restricts the intermediate surface's size, so it's a good idea * to use it. */ gdk_cairo_rectangle (cr, &area); cairo_clip (cr); /* Now push a group to change the target */ cairo_push_group (cr); cairo_set_source_surface (cr, surface, dx, dy); cairo_paint (cr); /* Now copy the intermediate target back */ cairo_pop_group_to_source (cr); cairo_paint (cr); cairo_destroy (cr); |
The surface here can be either an image surface or a X surface,
and can either be created on the spot or kept around for caching purposes.
Another alternative is to use pixbufs instead of surfaces with
gdk_cairo_set_source_pixbuf()
instead of cairo_set_source_surface()
.
The cairo developers plan to add self-copies in the future to allow
exactly this effect, so you might want to keep up on cairo
development to be able to change your code.
Using pango_cairo_show_layout() instead of gdk_draw_layout_with_colors().
GDK provided a way to ignore the color attributes of text and use
a hardcoded text color with the gdk_draw_layout_with_colors()
function. This is often used to draw text shadows or selections.
Pango’s cairo support does not yet provide this functionality. If
you use Pango layouts that change colors, the easiest way to achieve
a similar effect is using pango_cairo_layout_path()
and cairo_fill()
instead of gdk_draw_layout_with_colors()
. Note that this results in
a slightly uglier-looking text, as subpixel anti-aliasing is not
supported.