motifdeveloper.com
Sponsored by IST Limited
Formerly MW3: Motif on the World Wide Web [MW3 Logo]
Last Updated
January 25, 2005
 


X-Designer - The Leading X/Motif GUI Builder - Click to download a FREE evaluation
 

motifdeveloper.com
Home
About the site
Bulletin Board
News Archive
OpenGroup News
Events
Search
Frequently Asked Questions
The Motif FAQ
X/Motif FAQs
General FAQs
Ask Antony
Latest Q & A
All Q & As
Submit Question
Tips & Pointers
Code Examples
Software
OpenMotif
Widget Sets
GUI Toolkits & Libraries
Motif suppliers
Non-commercial S/W
Commercial Software
Multimedia
Miscellaneous
Organizations
Docs & Pubs
X/Motif Newsgroups
Security
Internationalization
Feedback
Feedback Form
Contributors
 

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

 

[IST Limited]
IST Limited is the
proud sponsor of motifdeveloper.com.
 

Thanks to all the contributors to these pages.

Privacy Policy

 
"Motif" is a registered trademark of The Open Group. All other trademarks are the property of their respective owners.

Some articles/papers here have their own copyright notice. All other information is copyright ©1999-2008 IST Limited

[CommerceOne] MW3 site originally by Ken Sall of Commerce One (formerly Century Computing).