Gtk Critical Error While Using Cairo and Pango.

Hello everyone,

I am having following example code which i am using to Draw background of a window plus a GtkImage On It. I am drawing background of GtkWindow by use of Cairo on Expose Event. I am Adding One GtkImage on this window and in that GtkImage i ma adding Text Using Pango but this image is not visible.

You can try and run this program.
It will just display window with given PNG image as background. but not the image on it. when i use export XLIB_SKIP_ARGB_VISUALS=1 on terminal then it displays both Window and Image on it but Transparency of background image is Lost.



#include <gtk/gtk.h>
#include "string.h"

gboolean DrawTextOnImage(GtkWidget *image, const gchar *markup, const gchar* fg_color)
    g_return_val_if_fail( image != NULL, FALSE );
    g_return_val_if_fail( markup != NULL, FALSE );

    gint x, y, pwidth, pheight;
    GdkGC *gc;
    GdkPixmap *pixmap;
    GdkPixbuf *pixbuf;
    GdkColor  color;
    GdkBitmap *mask;
    PangoRectangle  extents;
    GtkRequisition req;

    pixbuf = gtk_image_get_pixbuf( GTK_IMAGE(image) );
    gdk_pixbuf_render_pixmap_and_mask( pixbuf, &pixmap, &mask, 1 );

    if (fg_color == NULL)
        gdk_color_parse("black", &color);
        gdk_color_parse(fg_color, &color);

    gc = gdk_gc_new( GDK_DRAWABLE( pixmap ) );
    gdk_gc_set_rgb_fg_color(gc, &color);

    PangoLayout *layout;
    layout = pango_layout_new( gtk_widget_get_pango_context(image));
    pango_layout_set_markup( layout, markup, strlen(markup) );
    pango_layout_get_pixel_extents (layout, &extents, NULL);

    gtk_widget_size_request( image, &req );

    x = image->allocation.x;
    y = image->allocation.y;

    gdk_draw_layout( GDK_DRAWABLE(pixmap),
             x + (req.width - extents.width) / 2,
             y + (req.height - extents.height) / 2,

    g_object_unref (G_OBJECT (gc));
    g_object_unref (G_OBJECT (layout));

    GdkColormap *cmap = NULL;
    cmap = gtk_widget_get_colormap(image);
    gdk_drawable_get_size(pixmap, &pwidth, &pheight);

    pixbuf = gdk_pixbuf_get_from_drawable( NULL, pixmap, cmap,
             0 , 0,
             0 , 0,
             pwidth, pheight );

    g_object_unref( pixmap );

    GdkPixmap *new_pixmap;
    gdk_pixbuf_render_pixmap_and_mask( pixbuf, &new_pixmap, NULL, 1 );

    gtk_image_clear( GTK_IMAGE(image) );
    gtk_image_set_from_pixmap( GTK_IMAGE(image), new_pixmap, mask );

    g_object_unref( pixbuf );
    g_object_unref( cmap );

    return TRUE;

G_MODULE_EXPORT gboolean window_expose_cb(GtkWidget *widget, GdkEventExpose *event, gpointer data)
    char *img_path   = g_build_filename( "background.png", NULL);
    cairo_surface_t *g_imgGlass;
    g_imgGlass = cairo_image_surface_create_from_png( img_path );
    g_free( img_path );

    cairo_t *cr;
    cr = gdk_cairo_create(widget->window);
    cairo_set_source_surface(cr, g_imgGlass, 0, 0);
    cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);

   return FALSE;

int main( int argc, char *argv[])
    GtkWidget *window;
    GtkWidget *image;
    GdkScreen*      screen;
    GdkColormap*    colormap;

    gtk_init(&argc, &argv);

    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
    gtk_window_set_default_size(GTK_WINDOW(window), 350, 100);
    gtk_window_set_title(GTK_WINDOW(window), "XYZ");
    gtk_container_set_border_width(GTK_CONTAINER(window), 5);
    gtk_window_set_resizable( GTK_WINDOW(window), FALSE );
    gtk_window_set_decorated(GTK_WINDOW(window), FALSE);
    gtk_widget_set_app_paintable(window, TRUE);
    gtk_window_set_opacity(GTK_WINDOW(window), 1);

    gtk_widget_set_events(window, GDK_EXPOSURE_MASK
              | GDK_ENTER_NOTIFY_MASK
              | GDK_LEAVE_NOTIFY_MASK
              | GDK_BUTTON_PRESS_MASK

    g_signal_connect(G_OBJECT(window), "expose-event", G_CALLBACK(window_expose_cb), NULL);
    g_signal_connect(G_OBJECT(window), "destroy", G_CALLBACK(gtk_main_quit), NULL );

    image = gtk_image_new_from_file("Button_normal.png");
    gtk_container_add(GTK_CONTAINER(window), image );

    DrawTextOnImage(image, "<span size='large'><b>Some Text</b></span>", "#FFFFFF" );

    screen       = gtk_widget_get_screen(window);
    colormap     = gdk_screen_get_rgba_colormap(screen);
    gtk_widget_set_colormap(window, colormap);


    return 0;

Some times i get these types of Warnings and Errors:

(gtkimage:4536): Gtk-CRITICAL **: gtk_widget_set_colormap: assertion `GDK_IS_COLORMAP (colormap)' failed
(gtkimage:6146): Gdk-WARNING **: Attempt to draw a drawable with depth 16 to a drawable with depth 32

This is how my out put is coming when i set XLIB_SKIP_ARGB_VISUALS=1 and when i set it to 0 i am not able to see the GtkImage on window.

This is the pout put screen shot Click Here
<image "src="" href="" target="_blank">"/>

why this must be happening can some one please provide some help on this issue?
I am really Stuck on this.
Any Expert for Cairo, Pango and Gtk+ here please help?



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]