How can I find the x and y coordinates of a mouse click inside a Motif 
XmDrawingArea widget?
      | 
     
     25-Jan-05 12:30 GMT
      |  
      
Question:
How can I find the x and y coordinates of a mouse click inside a Motif 
XmDrawingArea widget?
 
The x and y coordinates of a mouse click inside an XmDrawingArea widget 
can very easily be obtained by setting up an event handler to intercept the 
click and then extract the values from the event data structure.
 
The first step is to set up the event handler. Supposing our drawing area 
widget is called drawingArea1 we would make a call similar to the following 
after we have created the drawing area widget:
 
    XtAddEventHandler(drawingArea1, ButtonPressMask | 
         PointerMotionMask, False, myevent, (XtPointer) 0) ;
In the above example we are indicating that we are interested in being 
notified of mouse button press and mouse button motion events inside our 
drawing area. If pointer motion is not of interest to you simply remove the 
PointerMotionMask setting from the XtAddEventHandler arguments.
 
The code to handle the event is going to be contained the "myevent" 
function specified in the XtAddEventhandler call. Having set up the event 
handler we now need to write the code to identify the x and y coordinates 
at the time the mouse button was pressed by the user.
 
An example myevent function to do this is as follows:
 
    void myevent(Widget w, XtPointer client_data, XEvent *event, 
                     Boolean *continue_to_dispatch)
    {
         XButtonPressedEvent *bp = (XButtonPressedEvent *) event ;
         XPointerMovedEvent  *mn = (XPointerMovedEvent *) event ;
         if (event->type == ButtonPress)
                 fprintf (stderr, "ButtonPress at %d, %d\n", bp->x, bp->y);
         else if (event->type == MotionNotify)
                 fprintf (stderr, "Motion at %d, %d\n", mn->x, mn->y);
    }
The myevent function gets passed the event data and we simply extract the 
appropriate fields from the XEvent data structure to obtain the x and y 
coordinates. In addition, in the above example we are also tracking mouse 
movement inside the widget.
  
   
Sponsored
by X-Designer - The Leading X/Motif GUI Builder
- Click to download a FREE evaluation
 
 
 
Goto top of page
     |