Tuesday, February 16, 2010

Drawing a gnocl::pixbuf to a GdkDrawable (eg gnocl::drawingArea)

After the frustration of not (yet ?) getting Cairo to draw to the a gnocl::pixbuf, I thought that I'd take a go at drawing such a buffer to a drawable object. Well, this proved a little frustrating too and I'm relieved to have solved this problem (for me there has to be an achievement by the end of the day, I just can't relax with a problem unsolved). But, of course, the drawing only lasts until such time as the drawable is remapped. Anyway, the connections are made and its pretty well up to the script how the refresh process is handled. This is the Tcl code snippet:

set da1 [gnocl::drawingArea ]
set win1 [gnocl::window \
   -child $da1 \
   -widthRequest 200 \
   -heightRequest 200 \
   -allowShrink 0 \
   -allowGrow 0]
update ;# ensure that the widgets are drawn first

set pb4 [gnocl::pixBuf load "wjg.jpg"]
$pb4 rotate 45

$pb4 draw $da1 \
   -srcXY {0 0} \
   -destXY {0 0} \
   -ditherXY {0 0} \
   -width 100 \
   -height 100

This is the output:


This is the C code to handle the drawing process:

case DrawIdx:
{
/* draw the pixbuf to a GdkDrawable: ie. a drawing area */
GtkWidget *widget;
GdkDrawable *dest;
gint src_x, src_y;
gint dest_x, dest_y;
gint width, height;
gint x_dither, y_dither;

/* initialize the values with defaults */
src_x = 0;
src_y = 0;
dest_x = 10;
dest_y = 10;

x_dither = 0;
y_dither = 0;

/* what size of the buffer has to be drawn */
width = gdk_pixbuf_get_width ( pixbuf );
height = gdk_pixbuf_get_height ( pixbuf );

widget = gnoclGetWidgetFromName ( Tcl_GetString ( objv[2] ), interp );

if ( widget == NULL )
{
return TCL_ERROR;
}

dest = GDK_DRAWABLE ( widget->window );

/* change values based upon switches passed */

gdk_draw_pixbuf (
    dest,
    gdk_gc_new ( widget->window ),
    pixbuf,
    src_x, src_y,
    dest_x, dest_y,
    width,
    height,
    GDK_RGB_DITHER_NONE,
    x_dither, y_dither );
}
break;

No comments: