拖放,motion_notify_event,鼠标坐标抖动

时间:2013-04-21 06:30:25

标签: c drag-and-drop gtk

然后我开始拖动一个图标(下面的代码),它开始在两个位置之间跳跃。

有人可以帮我解决这个震动问题吗?

代码:

#include <stdlib.h>
#include <stdio.h>
#include <gtk/gtk.h>
#include <gdk-pixbuf/gdk-pixbuf.h>
#include <string.h>

#define ICON_BUFFER 5



struct Icon {
  GtkWidget *image;
  int x,y,width,height;
};

GtkWidget *window;
GtkWidget *fixed;
struct Icon icons[ICON_BUFFER];

gboolean drag = FALSE;

static gboolean handleMove( GtkWidget *widget, GdkEventButton *event, gpointer data ){
  struct Icon *icon = (struct Icon*) data;

  icon->x = (int)event->x;
  icon->y = (int)event->y;
  gtk_fixed_move (GTK_FIXED (fixed), widget, icon->x, icon->y); 
  printf("move (%d , %d) %d\n", icon->x, icon->y,event->time);
  return TRUE;
}


static gboolean handleRelease( GtkWidget *widget, GdkEventButton *event, gpointer data ){
  g_signal_handlers_disconnect_by_func(widget, handleMove, data);
  printf("%s\n", "disconnect_handleMove");
  struct Icon *icon = (struct Icon*) data;
  printf("position (%d , %d)\n", icon->x, icon->y);
  drag = FALSE;
  return TRUE;
}

static gboolean handleClick( GtkWidget *widget, GdkEventButton *event, gpointer data ){
  //g_print ("Event box clicked at coordinates %f,%f\n", event->x, event->y);
  struct Icon *icon = (struct Icon*) data;

  if(!drag){
    drag = TRUE;
    printf("%s\n", "connect_handleMove");
    g_signal_connect (G_OBJECT (widget), "motion_notify_event" ,G_CALLBACK(handleMove), data);
    g_signal_connect (G_OBJECT (widget), "button-release-event",G_CALLBACK(handleRelease), data);
    printf("position (%d , %d)\n", icon->x, icon->y);
    return TRUE;
  }else
     return TRUE;

}



int main( int   argc, char *argv[] ){
  /* GtkWidget is the storage type for widgets */
  GtkWidget *event;
  GtkWidget *image;
  GtkWidget *background;
  GtkStyle *style;
  GdkPixbuf *pixbuf;

  gint i;

  gtk_init (&argc, &argv);
  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  gtk_window_set_title (GTK_WINDOW (window), "Fixed Container");
  g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL);
  gtk_container_set_border_width (GTK_CONTAINER (window), 0);

  fixed = gtk_fixed_new ();
  gtk_container_add (GTK_CONTAINER (window), fixed);
  gtk_widget_set_size_request(fixed,480,480);
  gtk_widget_show (fixed);


  for (i = 0 ; i < 4 ; i++) {
    event = gtk_event_box_new ();
    image = gtk_image_new_from_file("test2.jpg");

    icons[i].image = image;
    icons[i].x = i*50;
    icons[i].y = i*50;
    icons[i].width = 50;
    icons[i].height = 50;

    gtk_container_add (GTK_CONTAINER (event), image);
    gtk_fixed_put (GTK_FIXED (fixed), event, i*50, i*50);
    gtk_event_box_set_above_child(GTK_EVENT_BOX(event), FALSE);
        g_signal_connect (G_OBJECT (event), "button_press_event",
                          G_CALLBACK (handleClick), (gpointer) &icons[i]);
    gtk_event_box_set_visible_window(GTK_EVENT_BOX(event),(gboolean)FALSE);
    gtk_widget_show (image);
    gtk_widget_show (event);
  }


  gtk_widget_show (window);
  gtk_main ();
  return 0;
}

代码可以编译并运行(或gtk + -2.0而不是gtk + -3.0):

cc pkg-config --cflags --libs gdk-pixbuf-2.0 gtk + -3.0 FixedDragAndDrop.c -o FixedDragAndDrop.o&amp;&amp; ./FixedDragAndDrop.o

0 个答案:

没有答案