xref: /freebsd-src/contrib/dialog/mousewget.c (revision a96ef4501919d7ac08e94e98dc34b0bdd744802b)
14c8945a0SNathan Whitehorn /*
2*a96ef450SBaptiste Daroussin  *  $Id: mousewget.c,v 1.25 2019/07/25 00:06:38 tom Exp $
34c8945a0SNathan Whitehorn  *
44c8945a0SNathan Whitehorn  *  mousewget.c -- mouse/wgetch support for dialog
54c8945a0SNathan Whitehorn  *
6*a96ef450SBaptiste Daroussin  *  Copyright 2000-2017,2019   Thomas E. Dickey
74c8945a0SNathan Whitehorn  *
84c8945a0SNathan Whitehorn  *  This program is free software; you can redistribute it and/or modify
94c8945a0SNathan Whitehorn  *  it under the terms of the GNU Lesser General Public License, version 2.1
104c8945a0SNathan Whitehorn  *  as published by the Free Software Foundation.
114c8945a0SNathan Whitehorn  *
124c8945a0SNathan Whitehorn  *  This program is distributed in the hope that it will be useful, but
134c8945a0SNathan Whitehorn  *  WITHOUT ANY WARRANTY; without even the implied warranty of
144c8945a0SNathan Whitehorn  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
154c8945a0SNathan Whitehorn  *  Lesser General Public License for more details.
164c8945a0SNathan Whitehorn  *
174c8945a0SNathan Whitehorn  *  You should have received a copy of the GNU Lesser General Public
184c8945a0SNathan Whitehorn  *  License along with this program; if not, write to
194c8945a0SNathan Whitehorn  *	Free Software Foundation, Inc.
204c8945a0SNathan Whitehorn  *	51 Franklin St., Fifth Floor
214c8945a0SNathan Whitehorn  *	Boston, MA 02110, USA.
224c8945a0SNathan Whitehorn  */
234c8945a0SNathan Whitehorn 
244c8945a0SNathan Whitehorn #include <dialog.h>
254c8945a0SNathan Whitehorn #include <dlg_keys.h>
264c8945a0SNathan Whitehorn 
274c8945a0SNathan Whitehorn static int
mouse_wgetch(WINDOW * win,int * fkey,bool ignore_errs)284c8945a0SNathan Whitehorn mouse_wgetch(WINDOW *win, int *fkey, bool ignore_errs)
294c8945a0SNathan Whitehorn {
304c8945a0SNathan Whitehorn     int mouse_err = FALSE;
314c8945a0SNathan Whitehorn     int key;
324c8945a0SNathan Whitehorn 
334c8945a0SNathan Whitehorn     do {
344c8945a0SNathan Whitehorn 
354c8945a0SNathan Whitehorn 	key = dlg_getc(win, fkey);
364c8945a0SNathan Whitehorn 
374c8945a0SNathan Whitehorn #if USE_MOUSE
384c8945a0SNathan Whitehorn 
394c8945a0SNathan Whitehorn 	mouse_err = FALSE;
402a3e3873SBaptiste Daroussin 	if (key == KEY_MOUSE) {
414c8945a0SNathan Whitehorn 	    MEVENT event;
424c8945a0SNathan Whitehorn 
434c8945a0SNathan Whitehorn 	    if (getmouse(&event) != ERR) {
44*a96ef450SBaptiste Daroussin 		mseRegion *p;
45*a96ef450SBaptiste Daroussin 
46f4f33ea0SBaptiste Daroussin 		DLG_TRACE(("# mouse-click abs %d,%d (rel %d,%d)\n",
47f4f33ea0SBaptiste Daroussin 			   event.y, event.x,
48f4f33ea0SBaptiste Daroussin 			   event.y - getbegy(win),
49f4f33ea0SBaptiste Daroussin 			   event.x - getbegx(win)));
504c8945a0SNathan Whitehorn 		if ((p = dlg_mouse_region(event.y, event.x)) != 0) {
514c8945a0SNathan Whitehorn 		    key = DLGK_MOUSE(p->code);
524c8945a0SNathan Whitehorn 		} else if ((p = dlg_mouse_bigregion(event.y, event.x)) != 0) {
534c8945a0SNathan Whitehorn 		    int x = event.x - p->x;
544c8945a0SNathan Whitehorn 		    int y = event.y - p->y;
554c8945a0SNathan Whitehorn 		    int row = (p->X - p->x) / p->step_x;
564c8945a0SNathan Whitehorn 
574c8945a0SNathan Whitehorn 		    key = -(p->code);
584c8945a0SNathan Whitehorn 		    switch (p->mode) {
594c8945a0SNathan Whitehorn 		    case 1:	/* index by lines */
604c8945a0SNathan Whitehorn 			key += y;
614c8945a0SNathan Whitehorn 			break;
624c8945a0SNathan Whitehorn 		    case 2:	/* index by columns */
634c8945a0SNathan Whitehorn 			key += (x / p->step_x);
644c8945a0SNathan Whitehorn 			break;
654c8945a0SNathan Whitehorn 		    default:
664c8945a0SNathan Whitehorn 		    case 3:	/* index by cells */
674c8945a0SNathan Whitehorn 			key += (x / p->step_x) + (y * row);
684c8945a0SNathan Whitehorn 			break;
694c8945a0SNathan Whitehorn 		    }
704c8945a0SNathan Whitehorn 		} else {
714c8945a0SNathan Whitehorn 		    (void) beep();
724c8945a0SNathan Whitehorn 		    mouse_err = TRUE;
734c8945a0SNathan Whitehorn 		}
744c8945a0SNathan Whitehorn 	    } else {
754c8945a0SNathan Whitehorn 		(void) beep();
764c8945a0SNathan Whitehorn 		mouse_err = TRUE;
774c8945a0SNathan Whitehorn 	    }
784c8945a0SNathan Whitehorn 	}
794c8945a0SNathan Whitehorn #endif
804c8945a0SNathan Whitehorn 
814c8945a0SNathan Whitehorn     } while (ignore_errs && mouse_err);
824c8945a0SNathan Whitehorn 
834c8945a0SNathan Whitehorn     return key;
844c8945a0SNathan Whitehorn }
854c8945a0SNathan Whitehorn 
864c8945a0SNathan Whitehorn int
dlg_mouse_wgetch(WINDOW * win,int * fkey)874c8945a0SNathan Whitehorn dlg_mouse_wgetch(WINDOW *win, int *fkey)
884c8945a0SNathan Whitehorn {
894c8945a0SNathan Whitehorn     return mouse_wgetch(win, fkey, TRUE);
904c8945a0SNathan Whitehorn }
914c8945a0SNathan Whitehorn 
924c8945a0SNathan Whitehorn int
dlg_mouse_wgetch_nowait(WINDOW * win,int * fkey)934c8945a0SNathan Whitehorn dlg_mouse_wgetch_nowait(WINDOW *win, int *fkey)
944c8945a0SNathan Whitehorn {
954c8945a0SNathan Whitehorn     return mouse_wgetch(win, fkey, FALSE);
964c8945a0SNathan Whitehorn }
97