Re: XForms: drawing on my canvas with the mouse

Steve Lamont (spl@szechuan.ucsd.edu)
Thu, 12 Feb 98 12:39:25 PST

# To subscribers of the xforms list from spl@szechuan.ucsd.edu (Steve Lamont) :

> How could I get the mouse to return the x and y values when the mouse
> button is pressed, and again when it is released. I am trying to get mouse
> interaction with a line algorithm. I want the user to be able to draw a
> line with the mouse, simply by dragging the mouse.

If you're using a Canvas, you will need to register a ButtonPress and
ButtonRelease handler and probably a MotionNotify handler as well:

fl_add_canvas_handler( canvas, ButtonPress,
button_event_handler, NULL );
fl_add_canvas_handler( canvas, ButtonRelease,
button_event_handler, NULL );

[...]

fl_add_canvas_handler( canvas, MotionNotify,
motion_event_handler, NULL );

fl_remove_selected_xevent( FL_ObjWin( canvas ), PointerMotionHintMask );

and write one or more appropriate handlers.

Note the fl_remove_selected_xevent() call. It is necessary to get
"smooth" MotionNotify events -- if you don't deselect the
PointerMotionHintMask, then you'll get many fewer MotionNotify events
and things will be very jerky.

I generally only register the MotionNotify event handler when the
button is pressed and unregister it when the button is released --
this saves my application from having to handle and throw away a lot
of extraneous events. To do this, you need to call

fl_addto_selected_xevent( FL_ObjWin( canvas ), OwnerGrabButtonMask );

Otherwise, you won't be able to register the PointerMotionHintMask
(kind of funky, I know, but that's X for you).

When the handler is called, you'll be handed an XEvent structure.
Cast and dereference it appropriately and you'll have the location of
the pointer in the Window.

Refer to your favorite Xlib manual for details on the innards of the
XEvent structures and to the XForms manual for details on the fl_*
function calls.

spl

_________________________________________________
To unsubscribe, send the message "unsubscribe" to
xforms-request@bob.usuf2.usuhs.mil or see
http://bob.usuf2.usuhs.mil/mailserv/xforms.html
XForms Home Page: http://bloch.phys.uwm.edu/xforms
List Archive: http://bob.usuf2.usuhs.mil/mailserv/list-archives/