Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!sun-barr!newstop!exodus!aha.Eng.Sun.COM!kasso From: kasso@aha.Eng.Sun.COM (Chris Kasso) Newsgroups: comp.windows.open-look Subject: Re: Double Clicks in XView Message-ID: <4039@exodus.Eng.Sun.COM> Date: 6 Dec 90 02:26:09 GMT References: Sender: news@exodus.Eng.Sun.COM Distribution: comp Organization: Sun Microsystems, Mt. View, Ca. Lines: 131 In article zoo@aps1.spa.umn.edu writes: >Howdy. I'm trying to write an application that needs to be able to >distinguish between single clicks and double clicks of a mouse button. >I am using XView 2, and this processing will be in the event procedure >of a canvas. > >Does anyone have sample code for this? Thanks. > >david d [zoo] zuhn Univ. of Minnesota Dept. of Astronomy >zoo@aps1.spa.umn.edu Automated Plate Scanner Project You probably want to use an interval timer to distinguish between double and single clicks of the mouse. Here's some code that should do what you want: #include #include #include #include short buttonState; main(argc,argv) int argc; char *argv[]; { void EventProc(); Xv_server server; Frame frame; Canvas canvas; server = xv_init(XV_INIT_ARGC_PTR_ARGV, &argc, argv, NULL); frame = xv_create(NULL, FRAME, XV_LABEL, "XView Example", XV_X, 200, XV_Y, 200, XV_WIDTH, 200, XV_HEIGHT, 200, NULL); canvas = xv_create(frame, CANVAS, XV_WIDTH, WIN_EXTEND_TO_EDGE, XV_HEIGHT, WIN_EXTEND_TO_EDGE, NULL); xv_set(canvas_paint_window(canvas), WIN_EVENT_PROC, EventProc, WIN_CONSUME_EVENTS, WIN_MOUSE_BUTTONS, WIN_RESIZE, WIN_REPAINT, NULL, NULL); buttonState = 0; window_fit(frame); xv_main_loop(frame); } void EventProc(window, event) Xv_Window window; Event *event; { switch (event_action(event)) { case ACTION_SELECT: if (event_is_down(event)) { struct itimerval timer; void ButtonTimer(); if (!buttonState) { timer.it_value.tv_sec = 0; timer.it_value.tv_usec = 200000; timer.it_interval.tv_sec = 0; timer.it_interval.tv_usec = 0; notify_set_itimer_func(window, ButtonTimer, ITIMER_REAL, &timer, NULL); buttonState = 1; } else { /* * Second button press happened before the timeout, this must * be a double click. */ notify_set_itimer_func(window, NOTIFY_FUNC_NULL, ITIMER_REAL, NULL, NULL); fprintf (stderr, "Double-Click on ACTION_SELECT\n"); buttonState = 0; } } break; case ACTION_ADJUST: if (event_is_down(event)) fprintf (stderr, "ACTION_ADJUST (down)\n"); else fprintf (stderr, "ACTION_ADJUST (up)\n"); break; case ACTION_MENU: if (event_is_down(event)) fprintf (stderr, "ACTION_MENU (down)\n"); else fprintf (stderr, "ACTION_MENU (up)\n"); break; case WIN_REPAINT: fprintf (stderr, "WIN_REPAINT\n"); break; case WIN_RESIZE: fprintf (stderr, "WIN_RESIZE\n"); break; } } void ButtonTimer(client, which) Notify_client client; int which; { /* * Timeout happened before the second button press. This is a * single click. */ notify_set_itimer_func(client, NOTIFY_FUNC_NULL, ITIMER_REAL, NULL, NULL); fprintf (stderr, "Single-Click on ACTION_SELECT\n"); buttonState = 0; } -- Chris Kasso Internet: kasso@Eng.Sun.COM Windows and Graphics Software uucp: {...}!sun!kasso Sun Microsystems, Inc.