GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Sat Aug 23, 2014 3:22 pm

All times are UTC




Post new topic Reply to topic  [ 7 posts ] 
Author Message
 Post subject: motion-notify-event issue
PostPosted: Wed Sep 19, 2012 9:33 am 
Offline
Familiar Face

Joined: Wed Sep 19, 2012 9:08 am
Posts: 10
Hi all,

I have some confusion about the motion-notify-event, The Gtk instruction are as follows:
The ::motion-notify-event signal is emitted when the pointer moves over the widget's GdkWindow.
I'm listening for the signal in my callback function. But I also have to press the mouse button for the motion to be detected.

I want to capture the coordinates of drawingarea() when the mouse is moved.

Thanks in advance.


Top
 Profile  
 
 Post subject: Re: motion-notify-event issue
PostPosted: Wed Sep 19, 2012 3:31 pm 
Offline
Never Seen the Sunlight

Joined: Wed Jul 23, 2008 10:31 am
Posts: 2406
Location: Slovenia
Hello and welcome to the GTK+ forums!

Do you have a sample code that demonstrates the failure? I wrote apps that use GtkWidget::motion-notify-event to monitor cursor's position and they all worked as expected.

Cheers,
Tadej


Top
 Profile  
 
 Post subject: Re: motion-notify-event issue
PostPosted: Thu Sep 20, 2012 1:09 am 
Offline
Familiar Face

Joined: Wed Sep 19, 2012 9:08 am
Posts: 10
Hi Tade, thanks for you help. Here's my code:

Code:
gboolean motion_notify_event(GtkWidget *widget, GdkEventMotion *event)
{
   int x ,y;
   gchar buf[30];
   x=event->x;
   y=event->y;
   
   if (x<width && x>=0 && y<height && y>=0)
   {
     sprintf(buf, "X: %d, Y: %d",x, y);
     gtk_window_set_title(window, buf);
   }

   return TRUE;
}

Code:
  void open_file (gchar *message)
{
       //===============...ellipsis==============//
       gtkimage=gtk_image_new_from_pixbuf (gdkpixbuf);
       event_box = gtk_event_box_new ();                                     
       gtk_container_add (GTK_CONTAINER (event_box), gtkimage);

       gtk_box_pack_start (GTK_BOX(vbox),event_box,TRUE,TRUE,0);

       g_signal_connect(event_box,"motion_notify_event", G_CALLBACK(motion_notify_event), NULL);
       gtk_widget_show(gtkimage);
       gtk_widget_show (event_box);


Is it possible that I use "MingW" tool or my Gtk version is too old (2.x). ?
Jay.


Top
 Profile  
 
 Post subject: Re: motion-notify-event issue
PostPosted: Thu Sep 20, 2012 9:11 pm 
Offline
Never Seen the Sunlight

Joined: Thu Mar 24, 2011 2:10 pm
Posts: 328
Location: Sydney, Australia
from http://developer.gnome.org/gtk/2.24/GtkWidget.html#GtkWidget-motion-notify-event:
Quote:
To receive this signal, the GdkWindow associated to the widget needs to enable the GDK_POINTER_MOTION_MASK mask.

Have you done this?
I have done this in gtk2 (but don't know about mingw) so have some example code https://github.com/pchilds/GtkPlot. Mine is for a custom widget so its a bit more involved. I also convert the widget space coordinates to plot saced coordinates.
Here's some of the code (first from the class definition https://github.com/pchilds/GtkPlot/blob/master/gtk2plot/gtkplotlinear.h):

Code:
...
struct _GtkPlotLinearClass
{
   GtkDrawingAreaClass parent_class;
   void (*moved) (GtkPlotLinear *plot);
};


then the code for the custom widget (https://github.com/pchilds/GtkPlot/blob/master/gtk2plot/gtkplotlinear.c)

Code:
G_DEFINE_TYPE (GtkPlotLinear, gtk_plot_linear, GTK_TYPE_DRAWING_AREA);
enum {MOVED, LAST_SIGNAL};
static guint gtk_plot_linear_signals[LAST_SIGNAL]={0};
...
static gboolean gtk_plot_linear_motion_notify(GtkWidget *widget, GdkEventMotion *event)
{
   GtkPlotLinearPrivate *priv;
   GtkPlotLinear *plot;
   gdouble dx, dy;

   priv=GTK_PLOT_LINEAR_GET_PRIVATE(widget);
   plot=GTK_PLOT_LINEAR(widget);
   dx=((event->x)-(priv->range.xj))/((priv->range.xn)-(priv->range.xj));
   dy=((priv->range.yn)-(event->y))/((priv->range.yn)-(priv->range.yj));
   if ((dx>=0)&&(dy>=0)&&(dx<=1)&&(dy<=1))
   {
      (plot->xps)=((priv->bounds.xmax)*dx)+((priv->bounds.xmin)*(1-dx));
      (plot->yps)=((priv->bounds.ymax)*dy)+((priv->bounds.ymin)*(1-dy));
      g_signal_emit(plot, gtk_plot_linear_signals[MOVED], 0);
   }
   return FALSE;
}

static void gtk_plot_linear_class_init(GtkPlotLinearClass *klass)
{
   GObjectClass *obj_klass;
   GtkWidgetClass *widget_klass;

   obj_klass=G_OBJECT_CLASS(klass);
   ...
   widget_klass=GTK_WIDGET_CLASS(klass);
   (widget_klass->motion_notify_event)=gtk_plot_linear_motion_notify;
   gtk_plot_linear_signals[MOVED]=g_signal_new("moved", G_OBJECT_CLASS_TYPE(obj_klass), G_SIGNAL_RUN_FIRST, G_STRUCT_OFFSET (GtkPlotLinearClass, moved), NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
}

static void gtk_plot_linear_init(GtkPlotLinear *plot)
{
   ...
   gtk_widget_add_events(GTK_WIDGET(plot), GDK_BUTTON_PRESS_MASK|GDK_POINTER_MOTION_MASK|GDK_BUTTON_RELEASE_MASK);
   ...
}


and finally its implementation (https://github.com/pchilds/GtkPlot/blob/master/gtk2plot/testplotlinear.c):

Code:
...
void pltmv(GtkPlotLinear *plt, gpointer data)
{
   gchar *str;
   
   str=g_strdup_printf("x: %f, y: %f", plt->xps, plt->yps);
   gtk_statusbar_push(GTK_STATUSBAR(statusbar), gtk_statusbar_get_context_id(GTK_STATUSBAR(statusbar), str), str);
   g_free(str);
}

int main(int argc, char *argv[])
{
   GtkPlotLinear *plt;
   ...
   plot=gtk_plot_linear_new();
   g_signal_connect(plot, "moved", G_CALLBACK(pltmv), NULL);
}


As you can see apart from the custom widget stuff, its pretty much doing the same things as your code only for the gtk_widget_add_events line. Btw I use the same code here for both gtk2 and 3.


Top
 Profile  
 
 Post subject: Re: motion-notify-event issue
PostPosted: Fri Sep 21, 2012 9:11 am 
Offline
Familiar Face

Joined: Wed Sep 19, 2012 9:08 am
Posts: 10
Paul~

I don't use the motion mask. I try to link the GDK_POINTER_MOTION_MASK mask page but it seems break.

Your code looks a bit difficult...(I'm a new hand in GTK :P),however thanks for your reply.

Jay.


Top
 Profile  
 
 Post subject: Re: motion-notify-event issue
PostPosted: Sat Sep 22, 2012 8:10 am 
Offline
Familiar Face

Joined: Wed Sep 19, 2012 9:08 am
Posts: 10
Hi Paul,

I just try to add the GDK_POINTER_MOTION_MASK to my code.
My mouse move can be detected (don't press button).

Thanks a lot~ ^^

Jay.


Top
 Profile  
 
 Post subject: Re: motion-notify-event issue
PostPosted: Sat Sep 22, 2012 1:04 pm 
Offline
Never Seen the Sunlight

Joined: Thu Mar 24, 2011 2:10 pm
Posts: 328
Location: Sydney, Australia
yes there are a lot of broken links to gdk in the gtk documentation. I think this is because they are maintained by different groups and don't communicate between each other, but in either case there seems to be a universal problem that every link to gdk is broken and you have to hunt them down yourself (autogenerated documentation systems are always fraught with such issues). It should point to http://developer.gnome.org/gdk/stable/gdk-Events.html#GdkEventMask.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group