-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathmouse.c
273 lines (247 loc) · 5.66 KB
/
mouse.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/*_ mouse.c Mon Jan 16 1989 */
/* $Header$ */
#include <stdio.h>
#include "ed.h"
#if MOUSE
#if IBMPC || _WIN32
#include <msmouse.h>
#include <disp.h>
#endif
#include <time.h>
static int clicks = 0; /* number of clicks we've seen */
static clock_t lastclick; /* time of last click */
static void mouse_tocursor(unsigned *px, unsigned *py);
static void mouse_findpos(unsigned col, unsigned row, struct WINDOW **p_wp,
struct LINE **p_lp, int *p_doto);
static void mouse_markdrag(struct WINDOW *wp, struct LINE *lp, int doto);
static int mouse_leftisdown();
static void mouse_windrag(struct WINDOW *wp);
/*********************************
* Call this routine in the main command loop.
* It handles any mouse commands.
* Returns:
* 0 no mouse input
* !=0 value is a keystroke
*/
int mouse_command()
{ unsigned x,y;
struct WINDOW *wp;
struct LINE *lp;
int status;
if (!mouse) /* if no mouse installed */
return 0;
status = msm_getstatus(&x,&y); /* read button status */
mouse_tocursor(&x,&y); /* collect real cursor coords */
if (status & 1) /* if left button is down */
{ int doto;
mouse_findpos(x,y,&wp,&lp,&doto);
if (wp && lp)
{ markcol = wp->w_startcol + x;
mouse_markdrag(wp,lp,doto);
}
else
{ if (wp)
{
curbp = wp->w_bufp;
curwp = wp;
/* Examine window action buttons */
switch (x)
{ case 0:
window_only(FALSE,1);
goto L2;
case 1:
window_split(FALSE,1);
goto L2;
case 2:
delwind(FALSE,1);
L2: update();
goto L1;
case 3:
do
{ window_mvup(FALSE,1);
update();
} while (mouse_leftisdown());
break;
case 4:
do
{ window_mvdn(FALSE,1);
update();
} while (mouse_leftisdown());
break;
default:
mouse_windrag(wp);
break;
}
update();
}
else
L1:
while (mouse_leftisdown()) /* wait till button goes up */
;
clicks = 0;
}
}
#if IBMPC || _WIN32
else if (status & 2) /* if right button is down */
{
return memenu_mouse(y,x);
}
#endif
return 0;
}
/*******************************
* Convert from Microsoft coordinates to proper cursor coordinates.
*/
static void mouse_tocursor(unsigned *px, unsigned *py)
{
#if IBMPC
if (disp_mode == 0 || disp_mode == 1)
*px /= 16;
else
*px /= 8;
*py /= 8;
#endif
}
/***************************
* Set cursor at row,col.
*/
static void mouse_findpos(unsigned col, unsigned row, struct WINDOW **p_wp,
struct LINE **p_lp, int *p_doto)
{ int r;
int i;
struct WINDOW *wp;
struct LINE *lp;
*p_wp = NULL;
*p_lp = NULL;
*p_doto = 0; /* default cases */
r = 0; /* starting row for window */
for (wp = wheadp; wp; wp = wp->w_wndp)
{
if (row <= r + wp->w_ntrows)
{
*p_wp = wp;
if (row == r + wp->w_ntrows)
return;
for (lp = wp->w_linep; lp != wp->w_bufp->b_linep; lp = lforw(lp))
{ if (row == r)
break;
r++;
}
*p_lp = lp;
/* Determine offset into line corresponding to col */
*p_doto = coltodoto(lp,wp->w_startcol + col);
return;
}
r += wp->w_ntrows + 1;
}
}
/*****************************
* Given an initial position, drag the mouse, marking
* text as we go.
*/
static void mouse_markdrag(struct WINDOW *wp, struct LINE *lp, int doto)
{ unsigned x,y;
curbp = wp->w_bufp;
curwp = wp;
if (lp != wp->w_dotp) /* if on different line */
{ wp->w_dotp = lp;
wp->w_flag |= WFMOVE;
}
wp->w_doto = doto;
wp->w_markp = lp;
wp->w_marko = doto; /* set new mark */
curwp->w_flag |= WFHARD;
update();
/* Continue marking until button goes up */
while (msm_getstatus(&x,&y) & 1)
{
mouse_tocursor(&x,&y); /* collect real cursor coords */
mouse_findpos(x,y,&wp,&lp,&doto);
if (wp && wp == curwp && lp)
{
if (wp->w_markp || lp != wp->w_dotp) /* if on different line */
{
wp->w_dotp = lp;
wp->w_flag |= WFMOVE;
}
wp->w_doto = doto;
}
else if (!wp ||
wp != curwp && wp->w_toprow > curwp->w_toprow ||
wp == curwp && !lp)
window_mvdn(FALSE,1);
else
window_mvup(FALSE,1);
curgoal = curwp->w_startcol + x;
lastflag |= CFCPCN; /* behave as if forwline() or backline() */
update();
}
/* If mark start and mark end match, then no mark */
if (wp->w_markp == wp->w_dotp &&
wp->w_marko == wp->w_doto)
{ clock_t thisclick;
wp->w_markp = NULL;
thisclick = clock();
if (clicks)
{
#if __GNUC__
if (thisclick - lastclick > CLOCKS_PER_SEC / 4)
#else
if (thisclick - lastclick > CLK_TCK/2)
#endif
clicks = 0;
}
clicks++;
lastclick = thisclick;
#if linux || __OpenBSD__ || __FreeBSD__ || __APPLE__
clicks = 0;
#endif
switch (clicks)
{ case 1:
break;
case 2:
word_select(FALSE,1);
goto L1;
case 3:
word_lineselect(FALSE,1);
L1:
mlerase();
update();
break;
default:
clicks = 0;
break;
}
}
else
clicks = 0;
}
/*****************************
* Given an initial position, drag the border of the window around.
*/
static void mouse_windrag(struct WINDOW *wp)
{ unsigned x,y;
struct LINE *lp;
int doto;
/* Continue dragging window until button goes up */
while (msm_getstatus(&x,&y) & 1)
{
mouse_tocursor(&x,&y); /* collect real cursor coords */
mouse_findpos(x,y,&wp,&lp,&doto);
if (wp && wp == curwp && lp ||
wp != curwp && wp && wp->w_toprow < curwp->w_toprow)
window_shrink(FALSE,1);
else if (!wp ||
wp != curwp && wp->w_toprow > curwp->w_toprow)
window_enlarge(FALSE,1);
update();
}
}
/*****************************
* Return !=0 if left button is down.
*/
static int mouse_leftisdown()
{ unsigned x,y;
return msm_getstatus(&x,&y) & 1;
}
#endif /* MOUSE */