The easiest solution to your problem is to use cairo, which provides the
abstraction that you are asking for. When using cairo your drawing routines
draw on a cairo surface, which may be any of different output devices,
including a gdk-window or a png.

Here's how you draw with cairo in gtk:

static gint
drawing_area_expose_event (GtkWidget *widget,
                           GdkEventExpose *event,
                           gpointer user_data)
{
    double width = widget->allocation.width, height = widget->
allocation.height;
    cairo_t* cr = gdk_cairo_create (widget->window);
    double x=0.1*width,  y=0.5*height;
    double x1=0.4*width, y1=0.9*height,
        x2=0.6*width, y2=0.1*height,
        x3=0.9*width, y3=0.5*height;

    cairo_move_to (cr,  x, y);
    cairo_curve_to (cr, x1, y1, x2, y2, x3, y3);

    cairo_stroke (cr);

    cairo_set_source_rgba (cr, 1,0.2,0.2,0.6);
    cairo_set_line_width (cr, 0.03 * width);
    cairo_move_to (cr,x,y);   cairo_line_to (cr,x1,y1);
    cairo_move_to (cr,x2,y2); cairo_line_to (cr,x3,y3);
    cairo_stroke (cr);


    return 0;
}

This is made gtk independent by wrapping all the cairo statements in a
routine, e.g. draw_on_my_surface(cairo_t*).

By creating a different cairo_t as follows:

    surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
                                          1000,
                                          1000);

    cr = cairo_create (surface);
    draw_on_my_surface(cr);
    cairo_surface_write_to_png (surface, "my-image.png");

That provides the X11-independence that you were asking for.

Regards,
Dov

On Nov 15, 2007 2:47 PM, Richard Boaz <[EMAIL PROTECTED]> wrote:

> Hello,
>
> I have a general question about how GTK+ can be used and after my
> investigations have lead me nowhere, I thought I would post to the list.
>
> I have an application that reads data from a database and renders
> probability density function plots thereof to the user's display via my
> gui program.
>
> Over time, the users expressed the desire to output these images to be
> viewable elsewise.  Using libpng and the appropriate GTK+ routines, it was
> easy to output these images to a .png file on disk (for viewing by a
> web-browser, for example).
>
> Continuing the idea, the users requested that the middle-man, i.e., the
> gui, be completely cut out of the process: produce a program that reads
> the data from the database and writes directly to .png files.
>
> I have done this without problem, I can read data, make images and output
> them to disk in .png format just fine, as long as there's a physical
> display available.
>
> The problem comes when I attempt to execute this job under cron (analysis
> data is created on a server, stored in a database, followed by a program
> to read this data and create images for automatic web display), or if
> executed directly on a server machine with no physical display (but with
> X11 and GTK+ installed).
>
> When doing in either manner, my gtk_init() fails since there is no
> (alleged) existence of a display.
>
> My questions are two:
>
> 1) when run under cron, there ma be, in fact, an X-server running (say,
> serving the login splash screen), just not for the executing user.  Is
> there any way to force a gtk+ program to connect to an X-server running on
> the same machine from within a cron execution?
>
> 2) more ideally, however, I would like to be able to write a program that
> does not in any way have to rely on an X-server instance running at all.
> All drawing routines in GTK+ demand a drawable, which, in turn, require
> that gtk_init() be called before-hand.
>
> So, in general, is it possible to write a drawing program using GTK+ that
> outputs a .png image without having to rely on the physical existence of a
> screen or an X-server?
>
> Or must I resort to using some other library or program?  Or am I missing
> something obvious here (which I hope)?
>
> thanks for any tips or pointers, this is really the final open question
> that will make the system I've been developing for the last three years
> (graphically, at least) complete.
>
> cheers,
>
> richard boaz
>
> _______________________________________________
> gtk-list mailing list
> [email protected]
> http://mail.gnome.org/mailman/listinfo/gtk-list
>
_______________________________________________
gtk-list mailing list
[email protected]
http://mail.gnome.org/mailman/listinfo/gtk-list

Reply via email to