Re: help: a small example of program with gtk3



you can find a sample program doing all of this, and more, here:

 https://gitlab.com/ivor/GTK3-Cairo

On Wed, Oct 21, 2015 at 1:45 PM, Reymond Dominique <reymond d labogeo pf> wrote:
Hi all


I am looking for an example of program with the new functions in gtk3.0, for zooming with the mouse  inside a window containing a plot of data (XY type): when pressing the left button of mouse: a rectangular box appears in the window, that we can enlarge or decrease with the mouse.

I have already made theses function in GTK2.0 that work well, but the passage to GTK3.0 with the cairo functions is not evident for me. In fact I have some difficulties to replace the old functions by the new ones in GTK3.0.

Here are below the 3 functions to update into GTK3.0:
press_event(), selection_event() and release_event()


thank you for any help

Dominique

///////////////////////////////////////////////////////////////////////

/* press_event, event that occurs when the user click on the drawing area
 * left button == zoom
 * middle button == function intercepts
 * right button == axes intercepts
 */
gboolean press_event(GtkWidget *widget, GdkEventButton *event, NZE *ps)
{
  GdkGC *gc;
  static gulong id2;
 
  switch(event->button)
  {
  case 1:    // zoom to the selection

    printf(" in press_event\n");

    // remove the motion event signal
    g_signal_handlers_disconnect_by_func(G_OBJECT(widget),
                                         G_CALLBACK(motion_event), (gpointer)ps);
 
    // store the press coordinates
    Curseur.X_down = (gint)event->x;
    Curseur.Y_down = (gint)event->y;


    //default gc of the window
    gc = gdk_gc_new(widget->window);

    if(ps->pixmap_zoom)   g_object_unref(ps->pixmap_zoom);

    // create a new pixmap of a size equal to the drawing area
    ps->pixmap_zoom = gdk_pixmap_new(widget->window,
                                     widget->allocation.width,
                                     widget->allocation.height,
                                     -1);
  
    // deprecated
    gdk_draw_drawable(
      ps->pixmap_zoom,
      gc,
      widget->window,
      0, 0,
      0, 0,
      widget->allocation.width ,
      widget->allocation.height);
 
/*
    // new code with cairo
     cairo_t *cr = gdk_cairo_create (widget->window);
   gdk_cairo_set_source_pixmap (cr,
                            ps->pixmap_zoom,
                            0,
                            0);
    cairo_paint(cr);

   moveto3(cr,Curseur.X_down, 0);
   lineto3(cr,Curseur.X_down,widget->allocation.height);
   cairo_stroke(cr);
 */ 
    // connect the motion signal so we see a rectangle
    id2 = g_signal_connect( widget,
                            "motion_notify_event",
                            G_CALLBACK(selection_event),
                            ps);


    //  gdk_window_set_cursor(widget->window, cursors[event->button]);

    g_signal_connect(G_OBJECT(widget),
                     "button_release_event",
                     G_CALLBACK(release_event),
                     (gpointer)ps);
    break;
  case 2:    // not yet in function
    break;
  case 3:    // not yet in function
    break;
  default:
    // g_assert_not_reached();
    break;
  }

  return FALSE;
}
//////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////
/* selection_event, this callback is registered after a mouse button has been
 * pressed in the drawing area, it erases the ?last? rectangle and draws
 * another to represent the area the user has selected. the callback is
 * unregistered when the user releases the mouse button
 */

  gboolean selection_event(GtkWidget *widget, GdkEventMotion *event, NZE *ps)
  {
    GdkGC *gc;

    GdkModifierType state;
    int x,y;

    gc = gdk_gc_new(widget->window);

    gdk_window_get_pointer (event->window, &x, &y, &state);
    if (state & GDK_BUTTON1_MASK)
    {

      Curseur.X_end = (gint) event->x;
      Curseur.Y_end = (gint) event->y;
    
      // warning deprecated
      gdk_draw_drawable( widget->window,
                        gc,
                        ps->pixmap_zoom,
                        0, 0,
                        0, 0,
                        widget->allocation.width,
                        widget->allocation.height);
     
      // set_color(gc,colormap,60,60,60);
   
  // deprecated function
    gdk_draw_rectangle(
    widget->window,
    gc,
    FALSE,
    get_startx(Curseur), 0,
    abs(Curseur.X_down - Curseur.X_end),
    widget->allocation.height);
 
    g_object_unref(gc);


 /*
  // new code cairo: doesn't work
    cairo_t *cr = gdk_cairo_create (widget->window);

  // cairo_translate(cr,  Curseur.X_down - Curseur.X_end,0);
   gdk_cairo_set_source_pixmap(cr, widget->window,0,0);

   cairo_fill(cr);
 //   cairo_paint (cr);
  //    cairo_stroke(cr);
  
   moveto3(cr,Curseur.X_end, 0);
   lineto3(cr,Curseur.X_end,widget->allocation.height);
*/
 
   /*
   // gdk_cairo_set_source_window(cr,widget->window,0,0);
    gdk_cairo_set_source_pixmap(cr,
                widget->window,
                0,0);
    // new code with cairo
       cairo_set_source_rgb(cr, 0, 0, 0);
       cairo_set_line_width(cr, 1);

      cairo_rectangle(
                    cr,
                    get_startx(Curseur), 0,
                    abs(Curseur.X_down - Curseur.X_end),
     widget->allocation.height);
 
 //     cairo_stroke(cr);
 */ 


    }
    return FALSE;
  }

  ////////////////////////////////////////////////////////////////////////////
/* release_event, event that occurs when the user releases button on the
 * drawing area
 */
gboolean release_event(GtkWidget *widget, GdkEventButton *event, NZE *ps)
{
  gint x_up, y_up;
  gdouble x1, x2;



  switch(event->button)
  {
  case 1:    // zoom to the selection

    /* remove the selection callback, and this callback */
    g_signal_handlers_disconnect_by_func(widget,
                                         G_CALLBACK(release_event), (gpointer)ps);
    g_signal_handlers_disconnect_by_func(widget,
                                         G_CALLBACK(selection_event),(gpointer)ps);

    // put the normal cursor back
    //  gdk_window_set_cursor(widget->window, cursors[0]);

    x_up = (gint)event->x;
    y_up = (gint)event->y;

    // check for zero width or zero height
    if(x_up == Curseur.X_down)
    {
      g_signal_connect( widget,
                        "motion_notify_event",
                        G_CALLBACK(motion_event),
                        ps);
      return TRUE;
    }

    x1 = (Curseur.X_down < x_up) ? real_x(Curseur.X_down,ps->ideb,ps->ifin,widget->allocation.width ):
         real_x(x_up,ps->ideb,ps->ifin,widget->allocation.width);
    x2 = (Curseur.X_down > x_up) ? real_x(Curseur.X_down,ps->ideb,ps->ifin,widget->allocation.width ):
         real_x(x_up,ps->ideb,ps->ifin,widget->allocation.width);
    //  y1 = (Curseur.Y_down > y_up) ? real_y(Curseur.Y_down) : real_y(y_up);
    //  y2 = (Curseur.Y_down < y_up) ? real_y(Curseur.Y_down) : real_y(y_up);


    reDrawSingleChan(ps->zone_signal, ps);


    //  g_signal_handler_unblock(widget,id_motion_event);

    g_signal_connect( widget,
                      "motion_notify_event",
                      G_CALLBACK(motion_event),
                      ps);
    break;
  case 2:    // not yet in function
    break;
  case 3:    // not yet in function
    break;
  default:
    g_assert_not_reached();
    break;
  }

  return FALSE;
}

///////////////////////////////////////////////////////////////////////////////


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




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