1/*
2 * $Id: mouse.c,v 1.20 2012/12/21 10:00:30 tom Exp $
3 *
4 * mouse.c -- mouse support for dialog
5 *
6 * Copyright 2002-2007,2012	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#if USE_MOUSE
28
29static int basex, basey, basecode;
30
31static mseRegion *regionList = NULL;
32
33/*=========== region related functions =============*/
34
35static mseRegion *
36find_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
47void
48dlg_mouse_setbase(int x, int y)
49{
50    basex = x;
51    basey = y;
52}
53
54void
55dlg_mouse_setcode(int code)
56{
57    basecode = code;
58}
59
60void
61dlg_mouse_mkbigregion(int y, int x,
62		      int height, int width,
63		      int code,
64		      int step_y, int step_x,
65		      int mode)
66{
67    mseRegion *butPtr = dlg_mouse_mkregion(y, x, height, width, -DLGK_MOUSE(code));
68    butPtr->mode = mode;
69    butPtr->step_x = MAX(1, step_x);
70    butPtr->step_y = MAX(1, step_y);
71}
72
73void
74dlg_mouse_free_regions(void)
75{
76    while (regionList != 0) {
77	mseRegion *butPtr = regionList->next;
78	free(regionList);
79	regionList = butPtr;
80    }
81}
82
83mseRegion *
84dlg_mouse_mkregion(int y, int x, int height, int width, int code)
85{
86    mseRegion *butPtr;
87
88    if ((butPtr = find_region_by_code(basecode + code)) == 0) {
89	butPtr = dlg_malloc(mseRegion, 1);
90	assert_ptr(butPtr, "dlg_mouse_mkregion");
91	butPtr->next = regionList;
92	regionList = butPtr;
93    }
94    if (butPtr != 0) {
95	butPtr->mode = -1;
96	butPtr->step_x = 0;
97	butPtr->step_y = 0;
98	butPtr->y = basey + y;
99	butPtr->Y = basey + y + height;
100	butPtr->x = basex + x;
101	butPtr->X = basex + x + width;
102	butPtr->code = basecode + code;
103    }
104    return butPtr;
105}
106
107/* retrieve the frame under the pointer */
108static mseRegion *
109any_mouse_region(int y, int x, int small)
110{
111    mseRegion *butPtr;
112
113    for (butPtr = regionList; butPtr; butPtr = butPtr->next) {
114	if (small ^ (butPtr->code >= 0)) {
115	    continue;
116	}
117	if (y < butPtr->y || y >= butPtr->Y) {
118	    continue;
119	}
120	if (x < butPtr->x || x >= butPtr->X) {
121	    continue;
122	}
123	break;			/* found */
124    }
125    return butPtr;
126}
127
128/* retrieve the frame under the pointer */
129mseRegion *
130dlg_mouse_region(int y, int x)
131{
132    return any_mouse_region(y, x, TRUE);
133}
134
135/* retrieve the bigframe under the pointer */
136mseRegion *
137dlg_mouse_bigregion(int y, int x)
138{
139    return any_mouse_region(y, x, FALSE);
140}
141
142#else
143void mouse_dummy(void);
144void
145mouse_dummy(void)
146{
147}
148#endif /* USE_MOUSE */
149