Re: drawing on offscreen surface

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

 



Thanks for your suggestion. I am new to gtk+ programming so I am not sure how the offscreen rendering should be done.
Can you please give me a code snippet?
I tried this as suggested, that is, to draw on "expose" signal handler using cairo but it still does not draw any rectangle.
Can anyone also tell me which library to link against in order to call gdk_cairo functions like    gdk_cairo_set_source_window() as it gives undefined reference even if I link with libcairo.so (1.10). I am not sure if these gdk_cairo functions is needed?

#include <gtk/gtk.h>

static cairo_surface_t *surface = NULL;

static gboolean
configure_event_cb (GtkWidget *widget, GdkEventConfigure *event, gpointer data){
printf("configure-event called\n");
    cairo_t *cr;
    surface = gdk_window_create_similar_surface (gtk_widget_get_window(widget),
                                                 CAIRO_CONTENT_COLOR, 1200, 800);
    cr = cairo_create(surface);
    cairo_set_source_rgb(cr, 1, 1, 1);
    cairo_paint(cr);
    cairo_destroy(cr);
    return TRUE;
}

static gboolean
draw_cb(GtkWidget *widget, cairo_t *cr, gpointer data)
{
    printf("width %d, height %d\n", widget->allocation.width, widget->allocation.height);
   
    cairo_set_source_surface(cr, surface, 0, 0);
    cairo_paint(cr);

    return FALSE;
}

static gboolean
expose_event (GtkWidget *widget, GdkEventExpose *event)
{
   cairo_t *cr;
   cr = cairo_create(surface);
   printf("x %d, y %d, width %d, height %d\n", event->area.x, event->area.y, event->area.width, event->area.height);
   cairo_set_source_rgb(cr, 1, 0, 0);
   cairo_rectangle(cr, event->area.x+100, event->area.y+100, event->area.width-100, event->area.height-100);
   //draw a red rectangle x=100,y=100, x+width=1100, y+height=700
   cairo_fill(cr);
   gtk_widget_queue_draw_area (widget, event->area.x+100, event->area.y+100, event->area.width-100, event->area.height-100);
}

int main(int argc, char* argv[])
{
    GtkWidget *da;

    // Initialize GTK+
    gtk_init(&argc, &argv);
    // Create an 800x600 window that will contain the browser instance
    GtkWidget *main_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_window_set_default_size(GTK_WINDOW(main_window), 1200, 800);

    da = gtk_drawing_area_new();
    gtk_widget_set_size_request(da, 1200, 800);  
    gtk_container_add(GTK_CONTAINER(main_window), da); // attach drawing area with main window
    g_signal_connect (da, "draw", G_CALLBACK(draw_cb), NULL);
    g_signal_connect (da, "expose-event", (GtkSignalFunc)expose_event, NULL);
    g_signal_connect (da, "configure-event", G_CALLBACK(configure_event_cb), NULL);
    gtk_widget_set_events( da, GDK_EXPOSURE_MASK);

    // Make sure the main window and all its contents are visible
    gtk_widget_show_all(main_window);
    // Run the main GTK+ event loop
    gtk_main();
    return 0;
}


On Wed, Aug 8, 2012 at 6:29 PM, Paul Davis <paul@xxxxxxxxxxxxxxxxxxxxx> wrote:


On Wed, Aug 8, 2012 at 7:53 AM, Prasanta Sadhukhan <psadhukhan@xxxxxxxxx> wrote:
Hi,

I am trying to draw something on offscreen surface and then trying to render it on primary window. I started with simple primitives like rectangle but it seems to not draw anything except a blank white window. Here's my code.
Can anyone suggest as to what I need to do to draw a rectangle on an offscreen surface and then render to primary visible window?

first of all, you need to grasp the absolutely fundamental fact about drawing widget contents in GTK and more or less any other GUI toolkit: there is only one place you can do this, and that is in the callback/handler that notifies you that the window needs redrawing. In GTK2, that is done in a handler for the expose signal, and in GTK3 its done in a handler for the draw signal. You cannot draw into a widget's on-screen window anywhere other than the code in these handlers.

secondly, you should be using cairo for this (drawing into a cairo pattern and then using it as the source in the handler) or a GdkPixbuf. GdkPixmaps are now deprecated. 

finally, you're code never actually draws anything in the main window at all. you've just drawn into a pixmap, and then displayed a window.

_______________________________________________
gtk-list mailing list
gtk-list@xxxxxxxxx
https://mail.gnome.org/mailman/listinfo/gtk-list

[Index of Archives]     [Touch Screen Library]     [GIMP Users]     [Gnome]     [KDE]     [Yosemite News]     [Steve's Art]

  Powered by Linux