xref: /freebsd/contrib/dialog/mousewget.c (revision 8881d206f4e68b564c2c5f50fc717086fc3e827a)
1 /*
2  *  $Id: mousewget.c,v 1.25 2019/07/25 00:06:38 tom Exp $
3  *
4  *  mousewget.c -- mouse/wgetch support for dialog
5  *
6  *  Copyright 2000-2017,2019   Thomas E. Dickey
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU Lesser General Public License, version 2.1
10  *  as published by the Free Software Foundation.
11  *
12  *  This program is distributed in the hope that it will be useful, but
13  *  WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  *  Lesser General Public License for more details.
16  *
17  *  You should have received a copy of the GNU Lesser General Public
18  *  License along with this program; if not, write to
19  *	Free Software Foundation, Inc.
20  *	51 Franklin St., Fifth Floor
21  *	Boston, MA 02110, USA.
22  */
23 
24 #include <dialog.h>
25 #include <dlg_keys.h>
26 
27 static int
28 mouse_wgetch(WINDOW *win, int *fkey, bool ignore_errs)
29 {
30     int mouse_err = FALSE;
31     int key;
32 
33     do {
34 
35 	key = dlg_getc(win, fkey);
36 
37 #if USE_MOUSE
38 
39 	mouse_err = FALSE;
40 	if (key == KEY_MOUSE) {
41 	    MEVENT event;
42 
43 	    if (getmouse(&event) != ERR) {
44 		mseRegion *p;
45 
46 		DLG_TRACE(("# mouse-click abs %d,%d (rel %d,%d)\n",
47 			   event.y, event.x,
48 			   event.y - getbegy(win),
49 			   event.x - getbegx(win)));
50 		if ((p = dlg_mouse_region(event.y, event.x)) != 0) {
51 		    key = DLGK_MOUSE(p->code);
52 		} else if ((p = dlg_mouse_bigregion(event.y, event.x)) != 0) {
53 		    int x = event.x - p->x;
54 		    int y = event.y - p->y;
55 		    int row = (p->X - p->x) / p->step_x;
56 
57 		    key = -(p->code);
58 		    switch (p->mode) {
59 		    case 1:	/* index by lines */
60 			key += y;
61 			break;
62 		    case 2:	/* index by columns */
63 			key += (x / p->step_x);
64 			break;
65 		    default:
66 		    case 3:	/* index by cells */
67 			key += (x / p->step_x) + (y * row);
68 			break;
69 		    }
70 		} else {
71 		    (void) beep();
72 		    mouse_err = TRUE;
73 		}
74 	    } else {
75 		(void) beep();
76 		mouse_err = TRUE;
77 	    }
78 	}
79 #endif
80 
81     } while (ignore_errs && mouse_err);
82 
83     return key;
84 }
85 
86 int
87 dlg_mouse_wgetch(WINDOW *win, int *fkey)
88 {
89     return mouse_wgetch(win, fkey, TRUE);
90 }
91 
92 int
93 dlg_mouse_wgetch_nowait(WINDOW *win, int *fkey)
94 {
95     return mouse_wgetch(win, fkey, FALSE);
96 }
97