xref: /dragonfly/contrib/dialog/mouse.c (revision f9993810)
1 /*
2  * $Id: mouse.c,v 1.25 2022/04/03 22:38:16 tom Exp $
3  *
4  *  mouse.c -- mouse support for dialog
5  *
6  *  Copyright 2002-2017,2022	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 <dlg_internals.h>
25 #include <dlg_keys.h>
26 
27 #if USE_MOUSE
28 
29 static int basex, basey, basecode;
30 
31 static mseRegion *regionList = NULL;
32 
33 /*=========== region related functions =============*/
34 
35 static mseRegion *
36 find_region_by_code(int code)
37 {
38     mseRegion *butPtr;
39 
40     for (butPtr = regionList; butPtr; butPtr = butPtr->next) {
41 	if (code == butPtr->code)
42 	    break;
43     }
44     return butPtr;
45 }
46 
47 void
48 dlg_mouse_setbase(int x, int y)
49 {
50     basex = x;
51     basey = y;
52 }
53 
54 void
55 dlg_mouse_setcode(int code)
56 {
57     basecode = code;
58     DLG_TRACE(("# mouse_setcode %d\n", code));
59 }
60 
61 void
62 dlg_mouse_mkbigregion(int y, int x,
63 		      int height, int width,
64 		      int code,
65 		      int step_y, int step_x,
66 		      int mode)
67 {
68     mseRegion *butPtr = dlg_mouse_mkregion(y, x, height, width, -DLGK_MOUSE(code));
69     butPtr->mode = mode;
70     butPtr->step_x = MAX(1, step_x);
71     butPtr->step_y = MAX(1, step_y);
72 }
73 
74 void
75 dlg_mouse_free_regions(void)
76 {
77     while (regionList != 0) {
78 	mseRegion *butPtr = regionList->next;
79 	free(regionList);
80 	regionList = butPtr;
81     }
82 }
83 
84 mseRegion *
85 dlg_mouse_mkregion(int y, int x, int height, int width, int code)
86 {
87     mseRegion *butPtr;
88 
89     if ((butPtr = find_region_by_code(basecode + code)) == 0) {
90 	butPtr = dlg_calloc(mseRegion, 1);
91 	assert_ptr(butPtr, "dlg_mouse_mkregion");
92 	butPtr->next = regionList;
93 	regionList = butPtr;
94     }
95 
96     if ((butPtr->mode != -1) ||
97 	(butPtr->step_x != 0) ||
98 	(butPtr->step_y != 0) ||
99 	(butPtr->y != (basey + y)) ||
100 	(butPtr->Y != (basey + y + height)) ||
101 	(butPtr->x != (basex + x)) ||
102 	(butPtr->X != (basex + x + width)) ||
103 	(butPtr->code != basecode + code)) {
104 	DLG_TRACE(("# mouse_mkregion %d,%d %dx%d %d (%d)\n",
105 		   y, x, height, width,
106 		   butPtr->code, code));
107     }
108 
109     butPtr->mode = -1;
110     butPtr->step_x = 0;
111     butPtr->step_y = 0;
112     butPtr->y = basey + y;
113     butPtr->Y = basey + y + height;
114     butPtr->x = basex + x;
115     butPtr->X = basex + x + width;
116     butPtr->code = basecode + code;
117 
118     return butPtr;
119 }
120 
121 /* retrieve the frame under the pointer */
122 static mseRegion *
123 any_mouse_region(int y, int x, int small)
124 {
125     mseRegion *butPtr;
126 
127     for (butPtr = regionList; butPtr; butPtr = butPtr->next) {
128 	if (small ^ (butPtr->code >= 0)) {
129 	    continue;
130 	}
131 	if (y < butPtr->y || y >= butPtr->Y) {
132 	    continue;
133 	}
134 	if (x < butPtr->x || x >= butPtr->X) {
135 	    continue;
136 	}
137 	break;			/* found */
138     }
139     return butPtr;
140 }
141 
142 /* retrieve the frame under the pointer */
143 mseRegion *
144 dlg_mouse_region(int y, int x)
145 {
146     return any_mouse_region(y, x, TRUE);
147 }
148 
149 /* retrieve the bigframe under the pointer */
150 mseRegion *
151 dlg_mouse_bigregion(int y, int x)
152 {
153     return any_mouse_region(y, x, FALSE);
154 }
155 
156 #else
157 void mouse_dummy(void);
158 void
159 mouse_dummy(void)
160 {
161 }
162 #endif /* USE_MOUSE */
163