[goocanvas] Motion event compression



I'm trying to deactivate motion compression on goocanvas, but it dosen't have any effect.

I'm using goocanvas2.

 

The code to deactivate event compression:

 

gdk_win_canvas = gtk_widget_get_window(canvas);

gdk_window_set_event_compression(gdk_win_canvas, FALSE);

 

At the end I include the code sample that I'm using. With the same sample with GtkDrawingArea instead of GooCanvas I get in 10 seconds

1400 motion events (drawing included and with a stylus). With goocanvas, I get just 600 motion events (drawing excluded and also with a stylus) in 10 seconds. If I turn on event compression in the sample with GtkDrawingArea, I get also 600 motion events in 10 seconds, it seems to me that the call to gdk_set_event_compression() has no effect on GooCanvas.

Can anyone tell me what I'm doing wrong?

 

Code Sample 1 with GooCanvas (When the button is released the counted motion events are shown, this is the GtkDrawingArea example from gtk3 documentation a little bit ugly modified to work with goocanvas):

 

Compile: gcc `pkg-config --cflags gtk+-3.0` -I/usr/include/goocanvas-2.0 -o goo-canvas goo-canvas.c `pkg-config --libs gtk+-3.0` -lgoocanvas-2.0 (on Ubuntu)

 

Source:

 

#include <gtk/gtk.h>
#include <goocanvas-2.0/goocanvas.h>
 
/* Surface to store current scribbles */
static cairo_surface_t *surface = NULL;
 
static int motion_cnt = 0;
 
static void
clear_surface (void)
{
  cairo_t *cr;
 
  cr = cairo_create (surface);
 
  cairo_set_source_rgb (cr, 1, 1, 1);
  cairo_paint (cr);
 
  cairo_destroy (cr);
}
 
/* Create a new surface of the appropriate size to store our scribbles */
static gboolean
configure_event_cb (GtkWidget         *widget,
                    GdkEventConfigure *event,
                    gpointer           data)
{
  if (surface)
    cairo_surface_destroy (surface);
 
  surface = gdk_window_create_similar_surface (gtk_widget_get_window (widget),
                                               CAIRO_CONTENT_COLOR,
                                               gtk_widget_get_allocated_width (widget),
                                               gtk_widget_get_allocated_height (widget));
 
  /* Initialize the surface to white */
  clear_surface ();
 
  /* We've handled the configure event, no need for further processing. */
  return TRUE;
}
 
/* Redraw the screen from the surface. Note that the ::draw
 * signal receives a ready-to-be-used cairo_t that is already
 * clipped to only draw the exposed areas of the widget
 */
static gboolean
draw_cb (GtkWidget *widget,
         cairo_t   *cr,
         gpointer   data)
{
  cairo_set_source_surface (cr, surface, 0, 0);
  cairo_paint (cr);
 
  return FALSE;
}
 
/* Draw a rectangle on the surface at the given position */
static void
draw_brush (GtkWidget *widget,
            gdouble    x,
            gdouble    y)
{
  cairo_t *cr;
 
  /* Paint to the surface, where we store our state */
  cr = cairo_create (surface);
 
  cairo_rectangle (cr, x - 3, y - 3, 6, 6);
  cairo_fill (cr);
 
  cairo_destroy (cr);
 
  /* Now invalidate the affected region of the drawing area. */
  gtk_widget_queue_draw_area (widget, x - 3, y - 3, 6, 6);
}
 
/* Handle button press events by either drawing a rectangle
 * or clearing the surface, depending on which button was pressed.
 * The ::button-press signal handler receives a GdkEventButton
 * struct which contains this information.
 */
static gboolean
button_press_event_cb (GtkWidget      *widget,
                       GdkEventButton *event,
                       gpointer        data)
{
  motion_cnt=0;
  /* paranoia check, in case we haven't gotten a configure event */
  if (surface == NULL)
    return FALSE;
 
  if (event->button == GDK_BUTTON_PRIMARY)
    {
      draw_brush (widget, event->x, event->y);
    }
  else if (event->button == GDK_BUTTON_SECONDARY)
    {
      clear_surface ();
      gtk_widget_queue_draw (widget);
    }
 
  /* We've handled the event, stop processing */
  return TRUE;
}
 
/* Handle motion events by continuing to draw if button 1 is
 * still held down. The ::motion-notify signal handler receives
 * a GdkEventMotion struct which contains this information.
 */
static gboolean
motion_notify_event_cb (GtkWidget      *widget,
                        GdkEventMotion *event,
                        gpointer        data)
{
  int x, y;
  GdkModifierType state;
 
  g_print("motion\n");
  motion_cnt++;
  return TRUE;
}
 
static gboolean
button_release_event_cb (GtkWidget      *widget,
                       GdkEventButton *event,
                       gpointer        data)
{
  g_print("drawed %d points\n", motion_cnt);
  motion_cnt=0;
  return TRUE;
}
 
static void
close_window (void)
{
  if (surface)
    cairo_surface_destroy (surface);
}
 
static void
activate (GtkApplication *app,
          gpointer        user_data)
{
  GdkWindow *gdk_win_darea, *gdk_win_frame, *gdk_win_win;
  GtkWidget *window;
  GtkWidget *frame;
  //GtkWidget *drawing_area;
  GtkWidget *canvas;
 
  window = gtk_application_window_new (app);
  gtk_window_set_title (GTK_WINDOW (window), "Drawing Area");
 
  g_signal_connect (window, "destroy", G_CALLBACK (close_window), NULL);
 
  gtk_container_set_border_width (GTK_CONTAINER (window), 8);
 
  frame = gtk_frame_new (NULL);
  gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_IN);
  gtk_container_add (GTK_CONTAINER (window), frame);
 
  canvas = goo_canvas_new();
  gtk_widget_set_size_request(canvas, 100, 100);
  goo_canvas_set_bounds (GOO_CANVAS (canvas), 0, 0, 1000, 1000);
  gtk_container_add(GTK_CONTAINER(frame), canvas);
 
  /* Event signals */
  g_signal_connect (canvas, "motion-notify-event",
                    G_CALLBACK (motion_notify_event_cb), NULL);
  g_signal_connect (canvas, "button-release-event",
                    G_CALLBACK (button_release_event_cb), NULL);
  g_signal_connect (canvas, "button-press-event",
                    G_CALLBACK (button_press_event_cb), NULL);
 
  /* Ask to receive events the drawing area doesn't normally
   * subscribe to. In particular, we need to ask for the
   * button press and motion notify events that want to handle.
   */
  gtk_widget_set_events (canvas, gtk_widget_get_events (canvas)
                      // | GDK_POINTER_MOTION_HINT_MASK
                         | GDK_LEAVE_NOTIFY_MASK
                         | GDK_BUTTON_PRESS_MASK
                         | GDK_BUTTON_RELEASE_MASK
                         | GDK_POINTER_MOTION_MASK);
 
  gtk_widget_show_all (window);
 
  gdk_win_darea = gtk_widget_get_window(canvas);
  gdk_window_set_event_compression(gdk_win_darea, FALSE);
 
  gdk_win_frame = gtk_widget_get_window(frame);
  gdk_window_set_event_compression(gdk_win_frame, FALSE);
 
  gdk_win_win = gtk_widget_get_window(window);
  gdk_window_set_event_compression(gdk_win_win, FALSE);
}
 
int
main (int    argc,
      char **argv)
{
  GtkApplication *app;
  int status;
 
  app = gtk_application_new ("org.gtk.example", G_APPLICATION_FLAGS_NONE);
  g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
  status = g_application_run (G_APPLICATION (app), argc, argv);
  g_object_unref (app);
 
  return status;
}
 
Code Sample 2 with GtkDrawingArea (This is nearly the GtkDrawingArea example from gtk3 documentation):
 
Compile: gcc `pkg-config --cflags gtk+-3.0` -o drawing-area drawing-area.c `pkg-config --libs gtk+-3.0` (on Ubuntu)
 
Source:
 
#include <gtk/gtk.h>
 
/* Surface to store current scribbles */
static cairo_surface_t *surface = NULL;
 
static int motion_cnt = 0;
 
static void
clear_surface (void)
{
  cairo_t *cr;
 
  cr = cairo_create (surface);
 
  cairo_set_source_rgb (cr, 1, 1, 1);
  cairo_paint (cr);
 
  cairo_destroy (cr);
}
 
/* Create a new surface of the appropriate size to store our scribbles */
static gboolean
configure_event_cb (GtkWidget         *widget,
                    GdkEventConfigure *event,
                    gpointer           data)
{
  if (surface)
    cairo_surface_destroy (surface);
 
  surface = gdk_window_create_similar_surface (gtk_widget_get_window (widget),
                                               CAIRO_CONTENT_COLOR,
                                               gtk_widget_get_allocated_width (widget),
                                               gtk_widget_get_allocated_height (widget));
 
  /* Initialize the surface to white */
  clear_surface ();
 
  /* We've handled the configure event, no need for further processing. */
  return TRUE;
}
 
/* Redraw the screen from the surface. Note that the ::draw
 * signal receives a ready-to-be-used cairo_t that is already
 * clipped to only draw the exposed areas of the widget
 */
static gboolean
draw_cb (GtkWidget *widget,
         cairo_t   *cr,
         gpointer   data)
{
  cairo_set_source_surface (cr, surface, 0, 0);
  cairo_paint (cr);
 
  return FALSE;
}
 
/* Draw a rectangle on the surface at the given position */
static void
draw_brush (GtkWidget *widget,
            gdouble    x,
            gdouble    y)
{
  cairo_t *cr;
 
  /* Paint to the surface, where we store our state */
  cr = cairo_create (surface);
 
  cairo_rectangle (cr, x - 3, y - 3, 6, 6);
  cairo_fill (cr);
 
  cairo_destroy (cr);
 
  /* Now invalidate the affected region of the drawing area. */
  gtk_widget_queue_draw_area (widget, x - 3, y - 3, 6, 6);
}
 
/* Handle button press events by either drawing a rectangle
 * or clearing the surface, depending on which button was pressed.
 * The ::button-press signal handler receives a GdkEventButton
 * struct which contains this information.
 */
static gboolean
button_press_event_cb (GtkWidget      *widget,
                       GdkEventButton *event,
                       gpointer        data)
{
  /* paranoia check, in case we haven't gotten a configure event */
  if (surface == NULL)
    return FALSE;
 
  if (event->button == GDK_BUTTON_PRIMARY)
    {
      draw_brush (widget, event->x, event->y);
    }
  else if (event->button == GDK_BUTTON_SECONDARY)
    {
      clear_surface ();
      gtk_widget_queue_draw (widget);
    }
 
  /* We've handled the event, stop processing */
  return TRUE;
}
 
/* Handle motion events by continuing to draw if button 1 is
 * still held down. The ::motion-notify signal handler receives
 * a GdkEventMotion struct which contains this information.
 */
static gboolean
motion_notify_event_cb (GtkWidget      *widget,
                        GdkEventMotion *event,
                        gpointer        data)
{
  int x, y;
  GdkModifierType state;
 
  /* paranoia check, in case we haven't gotten a configure event */
  if (surface == NULL)
    return FALSE;
 
  if (event->is_hint) {
    gdk_window_get_device_position (event->window, event->device, &x, &y, &state);
  }
  else {
      x = event->x;
      y = event->y;
      state = event->state;
    }
 
  if (event->state & GDK_BUTTON1_MASK) {
    motion_cnt++;
    g_print("motion\n");
    draw_brush (widget, event->x, event->y);
  }
 
  /* We've handled it, stop processing */
  return TRUE;
}
 
static gboolean
button_release_event_cb (GtkWidget      *widget,
                       GdkEventButton *event,
                       gpointer        data)
{
  g_print("drawed %d points\n", motion_cnt);
  motion_cnt=0;
  return TRUE;
}
 
static void
close_window (void)
{
  if (surface)
    cairo_surface_destroy (surface);
}
 
static void
activate (GtkApplication *app,
          gpointer        user_data)
{
  GdkWindow *gdk_win_darea, *gdk_win_frame, *gdk_win_win;
  GtkWidget *window;
  GtkWidget *frame;
  GtkWidget *drawing_area;
 
  window = gtk_application_window_new (app);
  gtk_window_set_title (GTK_WINDOW (window), "Drawing Area");
 
  g_signal_connect (window, "destroy", G_CALLBACK (close_window), NULL);
 
  gtk_container_set_border_width (GTK_CONTAINER (window), 8);
 
  frame = gtk_frame_new (NULL);
  gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_IN);
  gtk_container_add (GTK_CONTAINER (window), frame);
 
  drawing_area = gtk_drawing_area_new ();
  /* set a minimum size */
  gtk_widget_set_size_request (drawing_area, 100, 100);
 
  gtk_container_add (GTK_CONTAINER (frame), drawing_area);
 
  /* Signals used to handle the backing surface */
  g_signal_connect (drawing_area, "draw",
                    G_CALLBACK (draw_cb), NULL);
  g_signal_connect (drawing_area,"configure-event",
                    G_CALLBACK (configure_event_cb), NULL);
 
  /* Event signals */
  g_signal_connect (drawing_area, "motion-notify-event",
                    G_CALLBACK (motion_notify_event_cb), NULL);
  g_signal_connect (drawing_area, "button-release-event",
                    G_CALLBACK (button_release_event_cb), NULL);
  g_signal_connect (drawing_area, "button-press-event",
                    G_CALLBACK (button_press_event_cb), NULL);
 
  /* Ask to receive events the drawing area doesn't normally
   * subscribe to. In particular, we need to ask for the
   * button press and motion notify events that want to handle.
   */
  gtk_widget_set_events (drawing_area, gtk_widget_get_events (drawing_area)
                         // | GDK_POINTER_MOTION_HINT_MASK
                         | GDK_LEAVE_NOTIFY_MASK
                                     | GDK_BUTTON_PRESS_MASK
                         | GDK_BUTTON_RELEASE_MASK
                                     | GDK_POINTER_MOTION_MASK);
 
  gtk_widget_show_all (window);
 
  gdk_win_darea = gtk_widget_get_window(drawing_area);
  gdk_window_set_event_compression(gdk_win_darea, FALSE);
  /*gdk_win_frame = gtk_widget_get_window(frame);
  gdk_window_set_event_compression(gdk_win_frame, FALSE);
  gdk_win_win = gtk_widget_get_window(window);
  gdk_window_set_event_compression(gdk_win_win, FALSE);*/
}
 
int
main (int    argc,
      char **argv)
{
  GtkApplication *app;
  int status;
 
  app = gtk_application_new ("org.gtk.example", G_APPLICATION_FLAGS_NONE);
  g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
  status = g_application_run (G_APPLICATION (app), argc, argv);
  g_object_unref (app);
 
  return status;
}

 

 



 
Gesendet mit Telekom Mail - kostenlos und sicher für alle!

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