How to improve performance of this example?

Hello, I'm new to the list, GTK+ and C. I'm building a Paint-like
application and was trying how to simulate the Brush tool with an
example to see how it performs. I read about GtkImage, GtkBitmap and
GtkPixmap but was recommended in #gtk+ over at to use a
GtkDrawingArea + Cairo since in GTK3 some of these were deprecated,
and it's encouraged to use cairo surfaces instead.

The problem with my example is, when you click and "brush" with the
mouse moving *too fast*, the signal doesn't come fast enough. What
could/would be faster? using pixbufs + GtkImage? I couldn't find
enough documentation about that, but that's why I am asking :)

Here's what I got (or if you prefer

Thank you!

----- canvas-test.c ------
#include <gtk/gtk.h>

int isdragging = 0;

/* Brush, paints a single pixel in coordinate x,y*/
void brush(cairo_t *cr, double x, double y){
  cairo_set_source_rgb(cr, 0, 0, 0);
  cairo_set_line_width (cr, 1);

gboolean handle_mouse_click(GtkWidget *widget, GdkEventButton *event,
gpointer user_data){
  brush(gdk_cairo_create(widget->window), event->x, event->y);
  isdragging = 1;
  return TRUE;

gboolean handle_mouse_release(GtkWidget *widget, GdkEventButton
*event, gpointer user_data){
  if (event->button == 1) {
    isdragging = 0;

  return TRUE;

gboolean handle_mouse_drag(GtkWidget *widget, GdkEventButton *event,
gpointer user_data){
  if (isdragging)
    brush(gdk_cairo_create(widget->window), event->x, event->y);
  return TRUE;

int main( int argc,
          char *argv[] )
  GtkWidget *window;
  GtkWidget *main_vbox;

  gtk_init (&argc, &argv);

  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  gtk_signal_connect (GTK_OBJECT (window), "destroy", GTK_SIGNAL_FUNC
(gtk_main_quit), "WM destroy");
  gtk_widget_set_usize (GTK_WIDGET(window), 300, 200);

  main_vbox = gtk_vbox_new (FALSE, 1);
  gtk_container_border_width (GTK_CONTAINER (main_vbox), 1);
  gtk_container_add (GTK_CONTAINER (window), main_vbox);
  gtk_widget_show (main_vbox);

  //gtkdrawable + cairo
  GtkWidget *canvas = gtk_drawing_area_new();
  gtk_widget_set_size_request (canvas, 500, 600);

  //gtk_widget_add_events(canvas, GDK_BUTTON_PRESS_MASK);
  gtk_widget_add_events(canvas, GDK_ALL_EVENTS_MASK);
  g_signal_connect(canvas, "button-press-event",
G_CALLBACK(handle_mouse_click), NULL);
  g_signal_connect(canvas, "button-release-event",
G_CALLBACK(handle_mouse_release), NULL);
"motion-notify-event",G_CALLBACK(handle_mouse_drag), NULL);

  gtk_box_pack_start (GTK_BOX (main_vbox), canvas, FALSE, TRUE, 0);

  //ends gtkdrawable + cairo example
  gtk_widget_show_all (window);
  gtk_main ();


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